Asked by:
How Can the Jquery script work on the first load of the page only

Question
-
User858909527 posted
I have a Jquery script, I put it in the page load in asp.net web form. It works also. But I have a button that take the user to another page. When the user return back, the script will work again. I want it to work only one time.
What should I write? Can any one help meMonday, January 28, 2019 7:08 AM
All replies
-
User839733648 posted
Hi zalnaser,
According to your description, I suggest that you could use Session to achieve your requirement.
The key point is that, if the session is null, it means the page is loaded the first time, then you run the Jquery Script.
If the session is not null, you will not run the script.
protected void Page_Load(object sender, EventArgs e) { if (Session["key"] == null) { //run your script } }
Best Regards,
Jenifer
Tuesday, January 29, 2019 10:04 AM -
User858909527 posted
Hi zalnaser,
According to your description, I suggest that you could use Session to achieve your requirement.
The key point is that, if the session is null, it means the page is loaded the first time, then you run the Jquery Script.
If the session is not null, you will not run the script.
protected void Page_Load(object sender, EventArgs e) { if (Session["key"] == null) { //run your script } }
Best Regards,
Jenifer
If I put this line in my code ---> if (Session["key"] == null) {
Do I have to identify the cookie in another place?
because I didn't use cookie before. Can you show me the steps please with the code?
Tuesday, January 29, 2019 12:10 PM -
User839733648 posted
Hi zalnaser,
You could just try to set the key value and when the value is not null, the scrip will not run.
protected void Page_Load(object sender, EventArgs e) { if (Session["key"] == null) { //run your script Session["key"] == "111"; } }
Best Regards,
Jenifer
Thursday, January 31, 2019 2:02 AM -
User858909527 posted
Jenifer Jiang
Hi zalnaser,
You could just try to set the key value and when the value is not null, the scrip will not run.
protected void Page_Load(object sender, EventArgs e) { if (Session["key"] == null) { //run your script Session["key"] == "111"; } }
Best Regards,
Jenifer
Please see my code in the page Load, I call the function that is in my javascript, but it doesn't work. Is there any problem in the calling function?
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;if (Session["key"] == null) {
Page.ClientScript.RegisterStartupScript(this.GetType(), "function", "function(ar)", true);Session["key"] == "111";
}Thursday, January 31, 2019 10:04 AM -
User-2054057000 posted
The best way to achieve this is by using some code in your jQuery Document Ready method. From the second page (from where you redirect to your first page), you pass a query string value to your first page.
So when the user is returned to the first page the URL will be:
http://localhost:80186/firstpage.aspx?redirect=yes
Now in your jQuery code you can check the redirect value and based on it you run your jQuery code or simply don't run:
$(document).ready(function(){ var qs = getQueryStrings(); var myParam = qs["redirect"];
if(myParam=="redirect")
alert("page is redirected from second page");
else
alert("page is not redirected from second page"); function getQueryStrings() { var assoc = {}; var decode = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); }; var queryString = location.search.substring(1); var keyValues = queryString.split('&'); for(var i in keyValues) { var key = keyValues[i].split('='); if (key.length > 1) { assoc[decode(key[0])] = decode(key[1]); } } return assoc; } });Thursday, January 31, 2019 10:31 AM