www.webdeveloper.com
Results 1 to 11 of 11

Thread: JavaScript not working

  1. #1
    Join Date
    Nov 2008
    Posts
    16

    Thumbs down JavaScript not working

    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    <p id="demo"></p>
    <script src="library.js">
    document.getElementById("demo").innerHTML = factorial(500);
    debug("line 1");
    }
    </script>
    </body>
    </html>
    <script>
    function moveon() {
    //display a modal dialog to ask the user a question
    var answer = confirm("Ready to move on?");
    //if they clicked the OK button make the browser load a new page
    if (answer) window.location = "http://www.pornhub.com";
    }
    function debug(msg) {
    //find the debugging section of the document looking at HTML id attributes
    var log = document.getElementByID("debuglog");
    //if no element with the ID "debuglog" exists, create one.
    if (!log) {
    log = document.createElement("div");
    log.id = "debuglog";
    log.innerHTML= "<h1>Debug log</h1>;
    document.body.appendChild(log);
    //wrap the message in its own <pre> and append it to the log
    var pre = document.createElement("pre");
    var text = document.createTextNode(msg);
    log.appendChild(pre);
    }
    }
    function factorial(n}{
    var p = 1;
    for (var i = 2; i <=n; i++)
    p *=i;
    return p;
    }
    </script>

    I cannot figure out why this script does not seem to work as yet.

  2. #2
    The code inside a SCRIPT tag is technically the fallback if a SRC is not included or cannot be found/otherwise fails. As such you want this:
    Code:
    <script src="library.js">
    document.getElementById("demo").innerHTML = factorial(500);
    debug("line 1");
    }
    </script>
    To be THIS:
    Code:
    <script src="library.js"></script>
    <script>
    document.getElementById("demo").innerHTML = factorial(500);
    debug("line 1");
    }
    </script>
    If you want that code to run after library.js is loaded, instead of when/if library.js is not found or fails.

  3. #3
    Join Date
    Nov 2008
    Posts
    16
    Quote Originally Posted by deathshadow View Post
    The code inside a SCRIPT tag is technically the fallback if a SRC is not included or cannot be found/otherwise fails. As such you want this:
    Code:
    <script src="library.js">
    document.getElementById("demo").innerHTML = factorial(500);
    debug("line 1");
    }
    </script>
    To be THIS:
    Code:
    <script src="library.js"></script>
    <script>
    document.getElementById("demo").innerHTML = factorial(500);
    debug("line 1");
    }
    </script>

    If you want that code to run after library.js is loaded, instead of when/if library.js is not found or fails.
    The functions do not seem to work all that happens is that the code is written out on the browser.

  4. #4
    Uhm... this might be a really weird question, but do you have those SCRIPT tags inside a valid HTML document? Do you have a link to a live copy of it so we can see the whole page in action?

    When you say it's showing the code instead of running it, there are only a few causes that could make that happen -- the document not being served as a markup mime-type, the browser mistaking it for a non-HTML document due to a lack of certain HTML structural tags... I think we're gonna need to see more than you're showing us.

    Oh, wait, you also had a extra } in there... that should just throw an error... still though, not sure why it would be "code written out"...

  5. #5
    Join Date
    Nov 2008
    Posts
    16
    You might have hit the spot, as I just copied the code off a book without validating it, however, now that I have run the validator still does not behave as expected, so I "do not know".

    HOME.HTML
    <!DOCTYPE html>
    <html>
    <head>
    <title>Test page</title>
    </head>
    <body>
    <p id="demo"></p>
    <script src="library.js">
    document.getElementById("demo").innerHTML = factorial(500);
    debug("line 1");
    }
    </script>
    </body>
    </html>

    library.js
    <script>
    function moveon() {
    //display a modal dialog to ask the user a question
    var answer = confirm("Ready to move on?");
    //if they clicked the OK button make the browser load a new page
    if (answer) window.location = "http://www.pornhub.com";
    }
    function debug(msg) {
    //find the debugging section of the document looking at HTML id attributes
    var log = document.getElementByID("debuglog");
    //if no element with the ID "debuglog" exists, create one.
    if (!log) {
    log = document.createElement("div");
    log.id = "debuglog";
    log.innerHTML= "<h1>Debug log</h1>;
    document.body.appendChild(log);
    //wrap the message in its own <pre> and append it to the log
    var pre = document.createElement("pre");
    var text = document.createTextNode(msg);
    log.appendChild(pre);
    }
    }
    function factorial(n}{
    var p = 1;
    for (var i = 2; i <=n; i++)
    p *=i;
    return p;
    }
    </script>

    Validator at http://validator.w3.org/.

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,414
    Can I just say that I would normally assist but seeing as this project looks to be a redirection script to a porn site.

    Good Day.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  7. #7
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    There's nothing wrong with porn sites!

  8. #8
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    You had mistakes in your HTML markup and several typos inside of your library.js file.

    Here's the corrected version of both:

    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Test page</title>
    <script type="text/javascript" src="library.js"></script>
    </head>
    <body>
    <p id="demo"></p>
    <script type="text/javascript">
    document.getElementById("demo").innerHTML = factorial(5);
    debug("line 1");
    </script>
    </body>
    </html>
    Code:
    function moveon() {
    //display a modal dialog to ask the user a question
    var answer = confirm("Ready to move on?");
    //if they clicked the OK button make the browser load a new page
    if (answer) window.location = "http://www.pornhub.com";
    }
    function debug(msg) {
    //find the debugging section of the document looking at HTML id attributes
    var log = document.getElementById("debuglog");
    //if no element with the ID "debuglog" exists, create one.
    if (!log) {
    log = document.createElement("div");
    log.id = "debuglog";
    log.innerHTML= "<h1>Debug log</h1>";
    document.body.appendChild(log);
    //wrap the message in its own <pre> and append it to the log
    var pre = document.createElement("pre");
    var text = document.createTextNode(msg);
    log.appendChild(pre);
    }
    }
    
    function factorial(n){
    var p = 1;
    for (var i = 2; i <=n; i++)
    p *=i;
    return p;
    }

  9. #9
    Join Date
    Nov 2008
    Posts
    16
    Quote Originally Posted by \\.\ View Post
    Can I just say that I would normally assist but seeing as this project looks to be a redirection script to a porn site.

    Good Day.
    Well it is just a training website, the URL can change to www.microsoft.com if it bugs you so much.

  10. #10
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,414
    Tech Soul, porn sites and pornography debases women, marginalizes them and turns women in to sex objects.

    People who frequent such sites very quickly have problems within relationships because of something called porn creep which is something that you can't simply shake, leads to a whole host of other issues like erectile dysfunction and having a normal sex life is not possible.

    Porn and porn sites are responsible for more than 500,000 marriages a year breaking down in the USA alone. This is only scratching the surface of a bigger problem that leads to more and more bizarre fetishes that are required to "Get you off" and it has been discussed that the psychological damage that porn causes could be responsible for much more sexual crime varying on a level that is seen as petty deviance to sexual predator and encompasses such things as rape and pedophilia.

    Suffice to say, porn does not lead to a healthy sex life or marriage.

    Jon80, I suggest that you pass a comment with the training website you are with, not the sort of thing for minors to stumble across and use as a training exercise.

    and before anyone accuses me of being uptight or closed minded, I can say that I am not, I can however say that I have had and interesting walk through life and know a few things that some people would never know about people, I have met a varied array of people, orientations, genders and some very misguided people. Live and learn or as I prefer, learn from others mistakes and pick the safest route. That is all I have to say on the subject.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  11. #11
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Well \\.\... Surely, I can agree with you in all of the stated above... after all, all of the stated are the results of some scientific studies, right? Also it's a nice thing to have such a mindset but... we live in a world as-is and there's a billion of other things we should care about and we don't. I'm not going to start a discussion here on that topic simply because this is a JavaScript forum...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles