2

I have the following function:

function parseLink(link){
 var newlink="";

 $.get(link,
   function(data){    

   startoffset = data.indexOf("location.replace") + 18;
   endoffset = data.indexOf("tiny_fold = 1;") - 8;
   newlink= data.substr(startoffset,(endoffset-startoffset));


});

return newlink;

 }

I'm using jquery $.get to parse a URL, which works fine if I do it without a function, but the function will return the empty string. Clearly I'm doing something wrong, but I don't know what; any help would be highly appreciated.

3
  • Are you requesting a URL from another domain? Commented Dec 20, 2011 at 21:09
  • possible duplicate of Javascript callback - how to return the result? Commented Dec 20, 2011 at 21:10
  • no, is from the same domain the URL will be sent to my backend to generate a security key Commented Dec 20, 2011 at 21:12

3 Answers 3

3

You'll need to pass in a function to be called when $.get returns. Something like:

function parseLink(link, callback) {
   $.get(link,
      function(data) {
         startoffset = data.indexOf("location.replace") + 18;
         endoffset = data.indexOf("tiny_fold = 1;") - 8;
         var newlink= data.substr(startoffset,(endoffset-startoffset));
         callback(newlink);
      });
 }

Then you can call it with:

parseLink('foo', function (newlink)
  {
     //Stuff that happens with return value
  }
);
Sign up to request clarification or add additional context in comments.

Comments

3

The call to $.get is asynchronous. See the control flow is like this:

parseUrl("http://www.test.com")
$.get(..., function callback() { /* this is called asynchronously */ })
return "";
... 
// sometime later the call to $.get will return, manipulate the
// newLink, but the call to parseUrl is long gone before this
callback();

I think what you meant to do is:

function parseUrl(link, whenDone) {
    $.get(link, function () {
        var newLink = "";
        // Do your stuff ...
        // then instead of return we´re calling the continuation *whenDone*
        whenDone(newLink);
    });
}

// Call it like this:
parseUrl("mylink.com", function (manipulatedLink) { /* ... what I want to do next ... */ });

Welcome to async spaghetti world :)

1 Comment

Oh by the way ... since you´re using jQuery anyway, you might have a look at api.jquery.com/jQuery.when . If you get your head around it it´s a bit easier (more linear) to read/write I think ...
0

Because the .get() operates asynchronously, parseLink() carries on executing and returns the empty newlink before the AJAX call has returned.

You'll need to trigger whatever is working with newlink from the callback, which may need you to rethink your implementation a little. What comes next (what should then happen to the populated newlink)?

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.