File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Soft Skills this week in the Jobs Discussion 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: 93
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: 61761
    
  67

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: 93
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: 61761
    
  67

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I dynamically load js library with document.write inside?