0

Does anyone know how I can make document.write with a script tag block execution?

<script type="text/javascript">
    var test = "bad";
    document.write('<script type="text/javascript" src="js.php"><\/script>');
    console.log(test);
</script>

Where js.php is

<?PHP sleep(1); echo "var test='good';"; ?>

console.log should output "good", but it outputs "bad". Of course, after a second, the var does change to "good", but I definitely need it to be blocking. Thanks!!

I kind of see whats happening. It looks like any document.write in the current script block actually gets written right after the current script block.

2 Answers 2

2

I seem to recall there being a workaround for writing <script> tags:

document.write('<scr'+'ipt type="text/javascript" src="js.php"><\/scr'+'ipt>');
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately it still doesn't work. I'm using chrome & firefox. Thanks anyway though!
1

Do an Ajax call to load more data. It is unlikely for this to be customizable. You can easily do that with jQuery and JSON.

3 Comments

You can define a callback to start your script only after the ajax call is completed. You can also add some "Loading" animation for your website while the ajax call takes place and remove it in the callback.
@nolanpro XHR can also be blocking, using the (a)synchronous flag of the XMLHttpRequest object. It's the third parameter of the open method. That's one of the reasons why 'AJAX' is not the right term for XHR by the way.
Thanks I think that will work! I can't use jQuery unfortunately because of the specific implementation, but XHR like Kooilnc said could be the tick.

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.