2

Suppose i have two html files, one.html and two.html. I want to include two.html in one.html, so i have done that like this,

<script>
        $(function(){
            $("#middle").load("two.html");
        });
</script>

I have a script file say, script.js which i am loading at the end of one.html as

<script src="scripts/script.js" ></script>

In that file I am writing function to be implemented on elements of two.html. But the functions are not being applied on two.html. So, what's the correct way of doing this?

1

4 Answers 4

2

Use $.get jQuery function

$.get( "tow.html", function( data ) {
  $( "#middle" ).html( data );
  alert( "Load was performed." );
});
Sign up to request clarification or add additional context in comments.

Comments

1

Why don't you use an (hidden?) iframe ? If it's located on the same server, you can access parent page (one.html)'s functions and elements:

window.parent.example_function();

or play with its DOM elements.

Comments

0

To add events on dynamically loaded elements you need to use .live() function in jquery < 1.9 and .on() if jquery >= 1.9

.live()

.on()

Comments

0

You can first load your second pages content and include the javascript only after the additional HTML was included.

$.get("two.html", function(data) {
    $('#middle').html(data);
    $('head').append($('<script>').attr('type', 'text/javascript').attr('src', 'scripts/script.js'));
});

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.