1

I know that jQuery automatically parses script elements and append them to the head, however I dont have much of a choice. I need to insert an html string exactly into a specified div. so for example <script src='http://.....'></script> into <div id="lb"></div>.

The problem is that the scripts get loaded from a server which I have no control over and is using a document.write() script. So if that gets appended to the header, there will be severe issues. How can I do this with or without jQuery?

7
  • 1
    If the script contains document.write, you will have issues, no matter where you insert it. It will replace the whole document. Commented Feb 16, 2012 at 13:49
  • @FelixKling It won't replace the whole document... It will insert the script at the end of the document. Commented Feb 16, 2012 at 13:50
  • 1
    @Willem: Maybe we are talking about different things. I was under the assumption that the script tag is dynamically generated or loaded, after the page was loaded. In that case, document.write will erase the current document. If the page is not fully parsed then document.write will just insert the content where it was called. But maybe I read too much into this question. Commented Feb 16, 2012 at 13:53
  • Are you talking about loading content/script with AJAX ? Commented Feb 16, 2012 at 13:54
  • No, basically the script loaded, renders HTML directly where the script is inserted. this script should not be loaded in the header. But i need to dynamically insert it into a specified div Commented Feb 16, 2012 at 13:59

3 Answers 3

1

Ok. So to rephrase your question:

You want to insert some < script > tags in your page. These tags load javascript files that have document.write() in them.

Now you want the document.write() to happen in some divs, and not in the header.

I think you would then need to render those script tags directly in the source of your page from the very start. Felix noted correctly that when you load thse script tags later, the whole page will be replaced by what is outputted by the document.write() function.

Thus, javascript or JQuery cannot load these script tags. You should render them serverside in the initial version of your page..!

Sign up to request clarification or add additional context in comments.

Comments

1

If you don't want your script to alter the content of you page you could insert it in an iframe instead of a div ? document should refer to the iframe then. I don't exactly get what you want though.

Comments

-1

Document.write() will put the script at the end of the document (afaik). Then, you would need to look-up these script-tags and put them into the proper DIVs (with a $(elm).append(scriptObj)).

But I might not understand what your problem is...

1 Comment

If the page was not fully parsed yet, document.write will insert the content at the place (or after) it was called. If the page was already parsed, it will replace the whole page. More information here: developer.mozilla.org/en/document.write

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.