This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes How can I dynamically load js library with document.write inside? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How can I dynamically load js library with document.write inside?" Watch "How can I dynamically load js library with document.write inside?" New topic
Author

How can I dynamically load js library with document.write inside?

Dmitry Zhuravlev
Ranch Hand

Joined: Apr 14, 2010
Posts: 91
Greetings Gentlemen!

1) My task is to load dynamically a javascript library. I know there is a proper way to do this: create script element and utilize the parentElement.appentChild method. This works, but there is a problem: the library I have to load contains document.write method. As a result, the parts related to this method (and specially parts where that script loads other scripts with document.write) do not work. Actually I see in chrom debugger that execution line reaches document.write, but there are no results.

If I load that library using HTML tag it works ok.

What can I do?
Whats the problem with document.write?

* * *

And also there is another question.
I need to send an ajax request to some external server. I tried to do this explicitly using jquery methods but this resulted in error: XMLHttpRequest cannot load ... origin is not allowed by Access-Control-Allow-Origin.

OK, I have googled a lot and found that I should use jquery.getJSON and callback=? to resolve this:

var url = 'http://some-long-external-url&callback=?';
$.getJSON(url, function(data) {
alert('success');
});

Finally, I have the following results:

1) without &callback= I see
XMLHttpRequest cannot load ... origin is not allowed by Access-Control-Allow-Origin.

2) with &callback=? the request is sent and the reply is received, BUT alert('success') doesn't run.

3) if &callback = mymethod is added than I have a result from p.1

Well, looks like I have tried all the ways known to me

Waiting for your advice!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60739
    
  65

Please don't ask two questions in a single post. Make separate posts.

1) document.write will only work at page load time. Besides, it's a dinosaur and should no longer be used. There are better ways to add elements to the DOM.

2) Ajax can only make requests to the same domain that served the page. You can fudge it by using JSONP (see the jQuery documentation for $.ajax) to make cross-domain requests. Be aware that JSONP is limited to GET requests.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dmitry Zhuravlev
Ranch Hand

Joined: Apr 14, 2010
Posts: 91
Bear Bibeault wrote:Please don't ask two questions in a single post. Make separate posts.
1) document.write will only work at page load time. Besides, it's a dinosaur and should no longer be used. There are better ways to add elements to the DOM.


Thanks for reply, Bear, but I cannot change the script with document.write because that script is downloaded from external site. Thats why I am asking this question here.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60739
    
  65

Then it's likely you will not be able to include the library other than at page load, or have to find a more modern library.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How can I dynamically load js library with document.write inside?
 
Similar Threads
ajax status code 0
Downloading controls in IE7.0
Issue regarding a basic AJAX project
Passing Value from Javascript method into HTML Body
Page section does not update after call of a4j:support in IE 9