views:

438

answers:

3

I need to get content from an external page and pass it as an argument to a function. I have looked into the url() method in the JQuery documentation but it seems it's only possible to use it to insert external content into div or some other HTML element.

Basically what I need to do is:

// I need to insert external page's content into the cont variable, how to do that?
var cont;
// so I can pass it to the bt() function (it's a tooltip plugin)
$('.class').bt(cont, {
    fill: '#2a4d6b',
    cssStyles: {color: 'orange', fontWeight: 'bold', width: 'auto'}
});

Can anyone tell me if something like that is possible?

+2  A: 
$.load("http://someplace", function(data){
    $('.class').bt(data, {
     fill: '#2a4d6b',
     cssStyles: {color: 'orange', fontWeight: 'bold', width: 'auto'}
    });
});

no?

also by external, how external? you cant get anything from another domain, otherwise that will work

mkoryak
Agree, element doesnt have to be part of dom
almog.ori
This will work but is arguably less efficient than .get which is designed to be used this way. Load should be used to load an element's contents, the callback is there so you can execute javascript on the loaded data.
Dennis Baker
It's on the same domain. Can I use a relative URI? Such as controller/action/id/53423?
Richard Knop
A: 

It should be possible. What should work (I haven't tried this myself) is to make a hidden IFRAME on the page. Then, set it's src to the URL you need to get, then use the DOM to access the IFRAME's contents. Comment back on this if any of that seems unclear to you.

machineghost
you haven't used jQuery have you
Scott Evernden
This is what AJAX is for; an iframe isn't necessary. Also, if anyone is thinking that it will work for cross-domain content, that's wrong too.
Justin Johnson
I've used jQuery extensively, just not the AJAX library. And if you read the original post, it doesn't say "Can anyone tell me if something like that is possible *IN JQUERY*?"; he just wanted to know if it was possible (and that was the part I was responding to).
machineghost
+1  A: 

You want to use .get (http://docs.jquery.com/Ajax/jQuery.get#urldatacallbacktype) insetad of load. It takes a callback function as an argument. The below will load a link and display it in an alert.

$.get('http://your.website.com/page.html', 
  function (data) {  alert(data) } );

your example rewritten:

$.get('http://your.website.com/page.html', 
      function (data) {  setClass(data); });

function setClass(cont) {
    $('.class').bt(cont, {
        fill: '#2a4d6b',
        cssStyles: {color: 'orange', fontWeight: 'bold', width: 'auto'}  
    });
}
Dennis Baker
There is some syntax error in the code above. Also, I'd like to ask, is it possible to to this for every .class element with different URI? The URI is very similar for all elements but with different id field (which is the actual id attribute of the element).
Richard Knop
I think that I've fixed his syntax error. He was missing a parens and semicolon after the "get" call.
Prestaul
You certainly can get different content for each .class, but this is going to be the least efficient way to do it. You don't want to do all of them in a loop because it would be wiser to just set the content when the page is initially loaded (no ajax), and if you want to load the content when the tooltip is loading then it will probably be a bad user experience (waiting for the tooptip while the content loads).
Prestaul
thanks for the edit.
Dennis Baker
Thanks, the problem is that there are many tooltips and most of them are dynamically generated from other pages, so the AJAX is the only way. Loading them all with PHP would make the page load much slower.What I have done is included a "Loading..." text in the tooltip while the external text is loading. And it doesn't take too long, 1-2 seconds and the tooltip is loaded.
Richard Knop