0

I am currently embedding a third-party javascript file directly on my page.

However, the website I'm embedding it from takes a while to respond so it stops the rendering of my site for several seconds.

I'm currently embedding it on my page on the spot where it will write some values:

<script language="javascript" type="text/javascript" src="[third-party site/file.js]"></script>

The response from the embedded script is just some JavaScript:

document.write('<div class="value">Value 1</div><div class="value">Value 2</div>');

I was thinking that after the page loads use jQuery to make an AJAX request and somehow parsing the response so I can get the values I need.

Is there be a better approach?

1

3 Answers 3

1

You can place this script inside a hidden element on the end of the body (just before </body>) and, after the page has loaded, move it to the desired location with jQuery.

    <div id="hiddenElement">
        <script type="text/javascript" src="[third-party site/file.js]"></script>
    </div>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#hiddenElement").appendTo("#someOtherElement");
        });
    </script>
</body>

Another solution would be to use jQuery .getScript() to load the script in the desired location as you said and @James McDonnell said.

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

Comments

0
$(document).read(function()
{
  $.getScript(/* src */)
});

This will run after the page has loaded.

$.getScript()

2 Comments

But if the return of the script is "document.write..." how will I be able to place the values in the location that I want. It seems that using $.getScript() will write the values where it was called from???
You could just place the <script> tags where you want them in your page. Other then that you're in a pickle.
0

Perhaps this could be part of your solution. Here is a stand-alone jQuery example where a button is used to trigger a change event on a DIV. This example just shows you how to access the html content of the div and to see how to manipulate/change it. This is not a solution for you but parts of it might be useful when combined with rcdmk's and James' ideas.

<html>
    <head>
        <!--<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>-->
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#button_test').click(function() {
                    $('.value').html('New stuff').change();
                });
                $('.value').change(function(){
                    $('div').each(function() {
                        alert($(this).html());
                    });
                });
            });
        </script>
    </head>
<body>


<div class="value">Value 1</div>
<div class="value">Value 2</div>
<button id="button_test">Click me</button>

Comments

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.