1

can I pass variable (variable named test in this case) to jQuery .load function?

$(document).ready(function() {  
$('a').click(function(e) {
    e.preventDefault();
    var test= $(this).attr('href');
        $('.window').load($(test));     

});

This doesn't work.

1
  • You mentioned that you wanted to load an external URL in one of your comments. But load is under a restriction such that "the request can not successfully retrieve data from a different domain, subdomain, or protocol." Commented Sep 8, 2011 at 16:22

2 Answers 2

4

You are wrapping your url in a call to jQuery. Just pass test, not $(test).

$(document).ready(function() {  
    $('a').click(function(e) {
        e.preventDefault();
        var test= $(this).attr('href');
        $('.window').load(test);        
    });
});

Also, you are missing the closing }); in your sample code. Not sure if it's a typo here or in your actual code.

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

Comments

2

In your code, test is a string. .load should take that string, not a jQuery object.

$('.window').load(test);

ought to work.

1 Comment

As a side note, however, it's probably a bad idea to use "window" as a class name. Even if it never conflicts with anything in JavaScript, it may mislead someone new who reads your code.

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.