shreyasree
Programmer
Hi all,
I need to dynamically load javascript code from a servlet, without overwriting the existing page.
I tried the following methods, but they don't seem to help:
Method 1
I tried to load it after page loads, by creating
var headID = document.getElementsByTagName("head")[0];
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.text = <javascript code obtained from servlet>
headID.appendChild(newScript);
This doesn't work probably because the obtained javascript has document.write.. which overwrites the existing page.
Method 2
I tried to include the javascript, by adding src as the servlet which returns the javascript code.
<script src=<path to servlet that returns the javascript code> type="text/javascript" language="javascript" ></script>
This gives a cross-site scripting error by the browser.
I need to load javascript code preferably before page loads, from a servlet. (Loading javascript after page loads would be fine too, if overwriting the existing page can be avoided)
It would be really great if you can help me figure this out!
Thanks!
Shreya
I need to dynamically load javascript code from a servlet, without overwriting the existing page.
I tried the following methods, but they don't seem to help:
Method 1
I tried to load it after page loads, by creating
var headID = document.getElementsByTagName("head")[0];
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.text = <javascript code obtained from servlet>
headID.appendChild(newScript);
This doesn't work probably because the obtained javascript has document.write.. which overwrites the existing page.
Method 2
I tried to include the javascript, by adding src as the servlet which returns the javascript code.
<script src=<path to servlet that returns the javascript code> type="text/javascript" language="javascript" ></script>
This gives a cross-site scripting error by the browser.
I need to load javascript code preferably before page loads, from a servlet. (Loading javascript after page loads would be fine too, if overwriting the existing page can be avoided)
It would be really great if you can help me figure this out!
Thanks!
Shreya