0

I use this script (does a document.write, you can visit the src to see it), which spends time loading as soon as it is reached in the HTML sequence of my pages:

<script type="text/javascript" src="http://pkmncards.disqus.com/recent_comments_widget.js?num_items=25"></script>

This prevents anything after the script from loading for a second or two while it loads, so instead I want to load it via jQuery Ajax to prevent the hangup.

Here's what I've attempted:

var $j = jQuery.noConflict();
$j(document).ready(function () {
    $j.ajax({
        url: "http://pkmncards.disqus.com/recent_comments_widget.js?num_items=25",
        dataType: "script",
        success: function (data) {
            $j("#recent-discussion").html(data)
        }
    });
});

However I'm not handling the success properly. I see the request load, but it doesn't do anything. I want it to run the script and output the document.write into the target #recent-discussion.

I'm still searching for examples but can't figure out what I'm doing wrong... any help is much appreciated!

7
  • 1
    Can't you simply move the script tag to the bottom? Commented Feb 26, 2013 at 20:01
  • 2
    You are turning a data type of "script" which is not HTML. Commented Feb 26, 2013 at 20:01
  • 1
    You're getting from datatype: "script" but trying to load the data directly as the html of a div? are you actually receiving a script, or is it html? Commented Feb 26, 2013 at 20:02
  • How about trying the async attribute on your script tag Commented Feb 26, 2013 at 20:04
  • @mgibsonbr I can only move it down so far, it displays HTML and I need that HTML in a certain place. Commented Feb 26, 2013 at 20:19

2 Answers 2

2

You cannot execute document.write after the page is loaded. Any script that you load via an AJAX call that uses document.write will not work correctly.

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

1 Comment

I think it might be a combination of this plus the fact that I'm trying to turn a script into HTML. Thank you for the responses everyone!
1

Your ajax call seems correct, and you don't actually need to do anything in the success callback - the script will have already started executing by then (but not necessarily so).

If it's not working, then check whether or not it was loaded correctly (maybe using an error callback in the ajax call). BTW is this script in the same domain as your main page? You can load scripts from different domains, but you can't normally make ajax calls to them (due to the same-origin policy).

I want it to run the script and output the document.write into the target #recent-discussion.

I don't think that's actually possible... If that's what your script need to do, and you can't change it, I'm afraid there's little you can do (short of monkey-patching document.write, that is).

1 Comment

Script is on a third party domain... yet another goof on my part.

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.