lols
lols

Reputation: 267

Execute script once for a page

I have added the following script to my asp.net master page

$(function() {

        $("#someDiv").someMethod(900, {
                chars: /\s/,
                doit: [" ( <a href='#' onclick='window.location.reload(true);' class='truncate_show'>more</a> . . . )", " ( . . . <a href='#' class='truncate_hide'>less</a> )"]
            });


        });

Now when the page loads first time, I see the link 'More'. On clicking 'More' a postback occurs and again the script gets fired. I want to stop executing the script once 'more' is clicked and the page gets reloaded.

Upvotes: 2

Views: 339

Answers (2)

Dave Ward
Dave Ward

Reputation: 60580

If you just want to set that up on the first load of the page, you might consider emitting it through ClientScript.RegisterStartupScript() in Page_Load, within a !IsPostBack conditional.

Ultimately, it might be better if you could keep the show/hide functionality on the client-side completely. It's always difficult drawing the lines between client and server.

If that's not helpful, update your question with a bit more code, and maybe I can give you a more specific answer.

Upvotes: 3

Ionise
Ionise

Reputation: 111

Try changing the default behavior of the link to not actually occur so that you're just getting the onclick event and also maybe after you fire the event try unbinding it using .die() (but you probably won't need that after you stop the link from going off)

Check out Events/die in the jQuery documentation.

Upvotes: 0

Related Questions