0

I have import an js file from our another domain by using the following code

<script src="http://webiduga.com/myJs.js"></script>

Which consists solely of:

 var a = 'Testing';

Then append that content to an existing div in the current html page

$(document).ready(function() {

    $('.alert.alert-quiz').html(a);

});

This code works great in Safari and IE, but it does not work in FF and Chrome. Do you have any idea why? The code is simple but it derived me crazy the last 2 hours.

Thanks!

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
renakre
  • 8,001
  • 5
  • 46
  • 99
  • Have you tried using `$(document).ready()`? If the code is simple, please post it. – Albert Xing Mar 07 '14 at 21:15
  • There's no obvious reason why that wouldn't work in some browsers but not others. Use the debugging tools in the browsers. Are there any errors in the JS console? Do you see the scripts being loaded (with the right and complete content) in the Net tab? – Quentin Mar 07 '14 at 21:15
  • 1
    While I'm not an expert on the subject, I strongly suspect this has to do with some browsers being more restrictive on cross-domain scripting and would start investigating there. – Sean Mar 07 '14 at 21:16
  • @smclark89 I thought there might be some security issues that I do not know. So, you guys think this code (importing js from external domain) should simply work in all browsers? – renakre Mar 07 '14 at 21:18
  • @erkaner are you allowed to post the link to the JS file so we can test it out? – Albert Xing Mar 07 '14 at 21:19
  • @AlbertXing I updated the code, the url is available now. – renakre Mar 07 '14 at 21:24
  • 1
    There are no cross-origin restrictions on simply loading a script. JSON-P — the pre-CORS work-around for the same origin policy — depends on that! – Quentin Mar 07 '14 at 21:26
  • @erkaner Everything works for me, I can write `alert(a)` after the script import. Make sure your script tag comes after the one with the URL. – Albert Xing Mar 07 '14 at 21:29
  • @Quentin After some quick reading, I'm pretty sure you're right. On a different note, this SO question appears to be addressing the OP's issue: http://stackoverflow.com/questions/10864732/javascript-accessing-variables-defined-in-external-js-files – Sean Mar 07 '14 at 21:31
  • Maybe, just MAYBE, the docready is triggering faster than the file download from your other domain? – RaphaelDDL Mar 07 '14 at 21:31
  • Why don't you use `innerText` instead since it is a text string? – SearchForKnowledge Mar 07 '14 at 21:32
  • @RaphaelDDL — impossible, script elements are blocking – Quentin Mar 07 '14 at 21:34

2 Answers2

1

I did a mockup for you and it's working for me in IE, Chrome, FF, Safari.

<!DOCTYPE html>

<html>

<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://webiduga.com/myJs.js"></script>
<script>
$(document).ready(function() {

    $('.alert').html(a);

});
</script>
</head>

<body>
<span class="alert"></span>
</body>

I am not sure what the .alert-quiz is for but does it matter if you leave it out?

SearchForKnowledge
  • 3,663
  • 9
  • 49
  • 122
-4

Maybe $('.alert.alert-quiz').html('a');? I'm not sure, I'm a rookie :S

Justplayit94
  • 385
  • 2
  • 19