javascript works in Chrome, but no in other browsers

Discussion in 'JavaScript' started by nocuser15, Mar 11, 2010.

  1. #1
    Hi people, I have the followin code:
    <html>
    <head>
    <title>My Page</title>
    </head>
    <BODY>
    <script id=url language=javascript src=""></script>
    <script type="text/javascript">
    document.getElementById("url").src = "http://www.mysite.com/mypage.aspx";
    </script>
    </body>
    </html>

    This code is working fine with Chrome, but it doesn't for other browser (IE, Firefox, Opera)... I've not been able to find where the problem is... IE gives me an error: internet explorer could not open the website. Operation aborted. The other browsers don't show any message.
    Could you help me with this?
    Thanks,
     
    nocuser15, Mar 11, 2010 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    it works but the html markup on your script tag is not valid, it needs type and no src at the start etc...
    this is called 'lazy-loading' - here is a working prototype for you: http://www.jsfiddle.net/cdZy7/

    also, if IE has such a problem then perhaps the script is affecting the dom state that it knows about (its not a deferred script...)
    sources:
    <script id="external_script" type="text/javascript"></script>
    HTML:
    
    document.getElementById('external_script').src = 'http://dci.uk.net/external.js';
    
    Code (javascript):
    consider this though, you may need to wait for the onload/onreadystatechange before you do anything when lazy-loading:
    http://wonko.com/post/painless_javascript_lazy_loading_with_lazyload
     
    dimitar christoff, Mar 12, 2010 IP
  3. nocuser

    nocuser Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks! it works for all the browsers (except IE)...
    Sorry, I didn't understand what I can do about it... Could you explain a bit more?
    Thanks again...
     
    nocuser, Mar 12, 2010 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    does the script you load have a document.write? if so, it wont work.

    basically, upon page loading, IE (or any browser) needs to evaluate the DOM (with all elements / nodes etc) and the javascript as well and how it can potentially affect said DOM. hence it's vital that they appraise if a script can potentially or just does modify the document by any means.
    loading the script in this way is kind of cheating as it introduces an unexpected new script after the DOM has already been released for manipulation (DOMREADY event).

    if memory serves, if the script being loaded has a document.write("") then it would output after the <script> tag in the source, which has already been appraised and mapped in events, styles and so forth. IE prefers to avoid certain types of dangers than to deal with them.

    i am not saying this is the reason why you are getting the problem, i can't see the script. but it may very well be. good luck
     
    Last edited: Mar 12, 2010
    dimitar christoff, Mar 12, 2010 IP