Injecting HTML into the DOM using script tag

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.

Answers


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:

<script type="text/javascript">
    document.write("<script language=\"javascript\" " +
        "src=\"oursite/script.php\"><\/script>");
</script>

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>"):

<script type="text/javascript" id="myScript">
    $(function () {
        $("#myScript").after("<script>").attr({
            language: "javascript",
            src: "oursite/script.php"
        });
    });
</script>

Need Your Help

iOS Casting to a BOOL

ios objective-c casting

Are these two lines of code different or the same (Edit: Do they do the same thing, typecast to a boolean?):

Google Map API Key Fail using PHP with MySQL

php mysql google-maps

I am using the following code to geocode address but hit their threshold. The code works fine outside of this. I signed up for the cloud services and enabled the free 60 day trial for the increased

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.