0

I m converting HREF dynamically to load some CSS like this

$(data).find("link").each(function () {
   var oldHref = $(this).attr("href");
   $(this).attr("href", "pdfviewer/RenderPreviewStyles?csspath=" + oldHref);
});

I want to call another function when HREF call is done, something like

$(this).attr("href", "pdfviewer/RenderPreviewStyles?csspath=" + oldHref)
 .success(function(){
    alert("Call other function");
});

But it is giving JS error

$(...).attr(...).success is not a function

Please suggest some way around..

Anil D
  • 1,989
  • 6
  • 29
  • 60
  • just call function name, whats the problem? – Hemal Feb 19 '16 at 12:19
  • http://stackoverflow.com/a/5371426/1414562 ??? – A. Wolff Feb 19 '16 at 12:23
  • @Hemal i'm getting js error $(...).attr(...).success is not a function.. – Anil D Feb 22 '16 at 06:18
  • Hemal means you dont need a success() function because here is nothing works like ajax (asyncron). Just call your function after $(this).attr('href', "pdfviewer/RenderPreviewStyles?csspath=" + oldHref); yourFunction(); – kaito Feb 22 '16 at 08:31

1 Answers1

1

You are using procedural functions. Just call the next one inside the each function. You only need to use a "success" function if you are performing some sort of asynchronous work.

$(data).find("link").each(function () {
   var oldHref = $(this).attr("href");
   $(this).attr("href", "pdfviewer/RenderPreviewStyles?csspath=" + oldHref);
   alert("Call other function");
});

jQuery's attr function is not asynchronous. Code execution will not continue until it has finished.

Nilpo
  • 4,675
  • 1
  • 25
  • 39
  • That's what this does. jQuery's `attr` function is not asynchronous. Code execution only continues after it has finished. – Nilpo Feb 22 '16 at 19:35
  • No way around what? This does exactly what you are asking. The second function will not be called until the HREF is changed. – Nilpo Feb 24 '16 at 10:03
  • It is not about just changing HREF, after HREF changed, a function call goes to Controller/action and want to call other function only when HREF call is done, got it? – Anil D Feb 25 '16 at 07:29
  • @AnilD Yes, I got it. I don't know any other way to say this more clearly. This does exactly what you want. The other function will not be called until the HREF is changed. – Nilpo Feb 25 '16 at 19:09