Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts

    effects of alert, besides alert itself ?

    1. dynamicaly load javascript file
    2. execute something from it --> fails (no script) coz it is not loaded yet.
    3. rerun 1 and 2 --> no results no errors

    1. dynamicaly load javascript file
    2. check DOM if it is there (while loop)
    3. execute something from it - --> fails (no script)
    4. rerun 1,2,3 --> no results no errors

    1. dynamicaly load javascript file
    2. alert('foo'), click OK
    3. execute - it works
    4. rerun 1,2,3 - it works.

    ?

    check code ...
    Code:
    var res = false;
              while (res == false)
              {    
                         
               var scripts = document.getElementsByTagName("script");
      
               // Look through them trying to find ourselves
               for(var i=0; i<scripts.length; i++) {
                    if(scripts[i].src.indexOf('name of script file') > -1) 
                    {
                      res = true;
                      break;
                    
                    }
                    else
                    {
                      res = false;
                     
                    }
               }
              }
    Last edited by BubikolRamios; 12-26-2013 at 04:08 AM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    How do you
    "dynamicaly load javascript file" ?

    The alert is allowing
    time for the DOM to
    include the dynamically
    load JavaScript file
    Last edited by DaveyErwin; 12-26-2013 at 04:01 AM.

  • #3
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts
    http://www.javascriptkit.com/javatut...criptcss.shtml

    The alert is allowing
    time for the DOM to
    include the dynamically
    load JavaScript file
    So ? That is obvious. But why it is not working without alert ?
    Last edited by BubikolRamios; 12-26-2013 at 04:06 AM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Quote Originally Posted by BubikolRamios View Post
    http://www.javascriptkit.com/javatut...criptcss.shtml



    So ? That is obvious. But why it is not working without alert ?
    because enough time for the DOM to
    include the dynamically
    load JavaScript file
    has not elapsed

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,619
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Presumably you have the script attached at the wrong place in the page. All JavaScript should go just before the </body> tag where it can run straight away because the page has already loaded.

    In my browser alert() has no effect whatever because I turned that obsolete debugging call off.
    Last edited by felgall; 12-26-2013 at 05:02 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts
    No, the script is in head part, look for "function loadDE(al)" ...
    Created test page: http://agrozoo.net/jsp/map.jsp?p1=us...ica%20granatum

    There you have two buttons at bottom(inside image).
    The one with alert does it's job(even many times repeated) if you have not run the one without alert before i.e. do refresh.

    Looking at js console, in some cases loades script is there but nothing shows up:

    This is like jquery(not familiar with that) code creating object ?
    Mybe somethinh overlaps ?

    Code:
      $(function(){
      $('#de-map').vectorMap({
        map: 'de_mill_en',....
    There are some html erros , can't figure what they mean, checked js there, all looks good.
    http://validator.w3.org/check?uri=ht...a%2520granatum
    Last edited by BubikolRamios; 12-26-2013 at 11:50 AM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #7
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by BubikolRamios View Post
    There you have two buttons at bottom(inside image).
    The one with alert does it's job(even many times repeated) if you have not run the one without alert before i.e. do refresh.

    Looking at js console, in some cases loades script is there but nothing shows up:
    The code that inserts the new script is:

    Code:
    function loadjscssfile(a,b){if(b=="js"){var c=document.createElement("script");c.setAttribute("type","text/javascript");c.setAttribute("src",a)}else{if(b=="css"){var c=document.createElement("link");c.setAttribute("rel","stylesheet");c.setAttribute("type","text/css");c.setAttribute("href",a)}}if(typeof c!="undefined"){document.getElementsByTagName("head")[0].appendChild(c)}}
    That code inserts a script element into the document without verifying that the script has been downloaded.

    You're executing the above code and, when the left button is clicked (without the alert), a scripting error is thrown which halts all further script execution because the script hasn't been downloaded yet. At this point, clicking either button is futile because script execution no longer works due to the unhandled error.

    When the right button is clicked (with the alert), script execution is blocked and, in the meantime, the browser has a chance to download the script. This prevents the error when execution resumes following dismissal of the alert box. Since the script is loaded at this point, clicking either button will no longer generate an error.

    Your current method of checking that the script has been loaded is to, apparently, infinitely execute a while loop. Not only is this not working, it's horrible for page performance. The page repeatedly freezes my browser, Firefox 26. (The bad performance may also be related, in part, to other parts of your code or the overuse of vector graphics on a single page.)

    You should eliminate that loop and do a proper check for script load using an event listener as in the following example:

    Code:
    <!doctype html>
    <html lang="en">
    	<head>
    		<meta charset="utf-8">
    		<title>Demo</title>
    		<style>
    			* { margin: 0; }
    			body { padding: 0 1rem; }
    			p { margin: 1rem 0; }
    		</style>
    	</head>
    	<body>
    		<script>
    			var script = document.createElement("script");
    			function doSomething() {
    				var paragraph = document.createElement("p");
    				paragraph.textContent = "The script was loaded successfully.";
    				document.body.appendChild(paragraph);
    			}
    			script.setAttribute("src", "js.js");
    			script.addEventListener("load", doSomething)
    			document.getElementsByTagName("head").item(0).appendChild(script);
    		</script>
    	</body>
    </html>
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • The Following 2 Users Say Thank You to Arbitrator For This Useful Post:

    BubikolRamios (12-26-2013), DaveyErwin (12-26-2013)

  • #8
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Quote Originally Posted by Arbitrator View Post
    in the meantime, the browser has a chance to download the script
    Thank You

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,619
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by BubikolRamios View Post
    No, the script is in head part
    ALL scripts belong at the bottom of the body. I know of only two exceptions - a framebreaker script and one that adds a class to the html tag. Your script is neither of those and so belongs at the bottom.

    alert() is now completely obsolete and should never be used.
    Last edited by felgall; 12-26-2013 at 07:06 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts
    What does validator/s say about that (js not beeing inside head section) ?

    I know some things does not work if you do not put js at the bottom of html.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #11
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by BubikolRamios View Post
    What does validator/s say about that (js not beeing inside head section) ?
    Nothing. script elements can occur within the body element.

    You should be using a <!doctype html> declaration signifying an HTML5 document, by the way, particularly if you're validating your code. Otherwise, the validator is going to be giving you useless information.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #12
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,619
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by BubikolRamios View Post
    I know some things does not work if you do not put js at the bottom of html.
    Only the two scripts I listed. Every other possible script will still work if it is placed at the bottom of the body. At a minimum moving it there means that the page appears to load faster. You can also get rid of any code in the script that is waiting for the page to finish loading when you put the script at the bottom (with the exception of scripts that are handling the situation where one or more of the images in the page fails to load) and have the script run sooner than it other wise would. Less code is needed in scripts placed at the bottom because there's no need to test if the HTML you want to interact with has loaded.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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