Dynamically load javascript code from servlet before page loads

Discussion in 'JavaScript' started by shreya s, Aug 31, 2007.

  1. #1
    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!

    Thanks!
    Shreya
     
    shreya s, Aug 31, 2007 IP
  2. garazy

    garazy Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hey,

    Not really sure what you are trying to do, but if you want you could try using the eval function.

    For example, at the bottom of your page you could have

    <script>
    eval ( [code from servlet] );
    </script>

    Gary
     
    garazy, Aug 31, 2007 IP
  3. shreya s

    shreya s Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your reply!

    Basically this is what I want to achieve:
    1. When user arrives at this specific page, client request Web server A for a bootstrap javascript;
    2. Web server A obtains the bootstrap javascript code from a common web server B, and returns the javascript code to the client.
    3. Client then includes this javascript code in the page.

    I tried using eval(<javascript code>), but since the eval is done after page loads, the document.write... in the <javascript code> overwrites the existing page, which is not desirable.

    I need to include the javascript bootstrap code (returned by the servlet) in a jsp page, without overwriting the existing page. (It is ok for the javascript to included either before page loads or after page loads)

    Any help is greatly appreciated.

    Thanks!
    Shreya
     
    shreya s, Aug 31, 2007 IP
  4. garazy

    garazy Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ok so potentially you could get the Javascript as an ajax request from the browser, which is returned and then eval'd. Is there a way that you could use document.getElementById ('id').innerHTML = [value] instead of doing a document.write? That might solve your problems.
     
    garazy, Aug 31, 2007 IP
  5. shreya s

    shreya s Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for your reply!

    I don't have control over the contents of the javascript as it is from a third party - so it may not be possible to modify the "docment.write" in it.

    Is there any other way out? Hope to hear from you...

    Thanks,
    Shreya
     
    shreya s, Sep 3, 2007 IP
  6. garazy

    garazy Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Start with something simple like this

    <body>
    this is the body of my doc
    <script type="text/javascrpit" src="servletGenerated.js">
    executeJS();
    </script>


    in servletGenerated.js do something like document.write ('hello');

    It shouldn't wipe out the page and go from there?
     
    garazy, Sep 4, 2007 IP
  7. shreya s

    shreya s Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi garazy,

    Thanks for your input - I figured out how to include the javascript via a servlet before the page loads. The following actually seems to do the job:

    <script src=<path to servlet that returns the javascript code> type="text/javascript" language="javascript" ></script>

    But, now my concern is that this javascript takes a long time to load, and hence the experience is not good if it is loaded during page load. (I also learnt that using 'DEFER" in the script tag only hints to the user agent to **optionally** defer interpreting the script - it doesn't mean that the javascript will be loaded only after the page loads).

    So, now I am back to solving the problem of how to load a 3rd party javascript (which has a document.write in it - which I cannot modify) after the page loads.

    Any suggestions?

    Thanks,
    Dhana
     
    shreya s, Sep 6, 2007 IP
  8. shreya s

    shreya s Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi garazy,

    Thanks for your input - I figured out how to include the javascript via a servlet before the page loads. The following actually seems to do the job:

    <script src=<path to servlet that returns the javascript code> type="text/javascript" language="javascript" ></script>

    But, now my concern is that this javascript takes a long time to load, and hence the experience is not good if it is loaded during page load. (I also learnt that using 'DEFER" in the script tag only hints to the user agent to **optionally** defer interpreting the script - it doesn't mean that the javascript will be loaded only after the page loads).

    So, now I am back to solving the problem of how to load a 3rd party javascript (which has a document.write in it - which I cannot modify) after the page loads - without overwriting the original contents of the page?

    Any suggestions?

    Thanks,
    Dhana
     
    shreya s, Sep 6, 2007 IP
  9. shreya s

    shreya s Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hi all,

    My inference is that no matter what technique is used to load a javascript (which has a document.write.. in it), if it is loaded after the page loads, it will overwrite the original contents of the page.

    In other words, if a javascript with a document.write is loaded after the page loads, it will definitely overwrite the original contents of the page.

    Regards,
    Shreya
     
    shreya s, Sep 7, 2007 IP
  10. Arindam Samanta

    Arindam Samanta Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hi,

    Did you solve the problem?

    Frm my html I am calling like below
    <script src="servlet/TestServlet.js" type="text/javascript" language="javascript" ></script>

    <input type="button" onclick="test()" value="Click Me" />

    And my servlet like below
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    out.println("<script type=\"text/javascript\" language=\"javascript\">");
    out.println("function test(){ ");
    out.println("alert(\"Wow! You seem really happy!\");}");

    out.println("</script>");

    }


    But test button is not calling when I clicked on 'Clicke Me' button?
    Servlet is calling when html page is loaded but javascript is including into the html page.

    Please help me how did you solve this problem?
     
    Arindam Samanta, Feb 14, 2011 IP