We allow users to place a
<script language="javascript" src="oursite/script.php"></script>
tag on their page which should then embed some content from our site into their site. Currently script.php contains document.write(“some content loaded from the database”), however there are some limitations.
Is there anyway I can have the same thing achieved using jQuery ? How do i tell jQuery to put a certain piece of HTML code EXACTLY where the script tag is ? document.write() can do this, but i’m not sure how to do this using jquery. (we are already providing the jquery js code to the client through script.php.
You don’t need jQuery to do a
document.write(). As long as it is executed inline (ie, not in an event handler such as$(document).ready()), it will work. Just make sure you escape the end script tag (like this:<\/script>), so that the HTML parser doesn’t mistake it for an actual end script tag:Alternatively, you could add the script using DOM manipulations. If you want to add the script after the page has loaded, this is your only option. To position it after the script tag that is calling it, give your script tag an id and use
$("#myScript").after("<script>"):