Click here to Skip to main content
15,888,287 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
Dear All,

I am trying to make div element display block in javascript. When using location.href, the div content comes and disappears immediately.

Following is my javascript function :

C#
function TestOne1() {
            window.location.href = "../Test.aspx?One=numberone";
            document.getElementById("divSub1").style.display = '';            
        }


How do I display the div content even after changing my URL using window.location.href.


Any help or suggestion would be greatly appreciated.

Thanks,
Posted
Updated 15-Mar-14 20:22pm
v2

So you're trying to display a div, but because you are changing the URL, the DOM is refreshing and your div is disappearing before you can really see it?

Try changing the div a few seconds before you navigate to the new URL, like this:
JavaScript
function TestOne1() {
    document.getElementById("divSub1").style.display = 'block';
    setTimeout(function() { window.location.href = "../Test.aspx?One=numberone"; }, 3000); 
}


This way, the div will be displayed, and three seconds later, the page will navigate.
 
Share this answer
 
v2
Comments
Raj.rcr 16-Mar-14 2:20am    
I tried this. But after 3 seconds, when URL changes, the div will again disappear.
Mitchell J. 16-Mar-14 2:34am    
Of course it will! :-)
Whenever you change the URL of the browser, it will navigate to that page, refresh the Dom, and in that process the entire screen will go blank...

Unless the div is created dynamically, it should reappear after the new page finishes loading. Does it?
Raj.rcr 16-Mar-14 2:39am    
You are right. But, my requirement is this. I want to display the div content after changing my URL because I am going to use the query string of the changed URL for some other purpose. Is there any alternate solution ? Thanks
Mitchell J. 16-Mar-14 2:46am    
I get a sense that you are not being completely transparent about what you are trying to do.
You should have posted what you just did as your original question.

I recommend posting a completely new question, in which you completely explain the process that you are trying to create, show what you are doing so far (but don't code dump), and see who is able to help you.
Raj.rcr 16-Mar-14 3:01am    
My question was clear. I think you did not understand it. I solved it myself by using history.pushState(null, null, '../Test.aspx?One=numberone'); . It actually chnages the URL without reloading the page and div will not disappear. Thanks
history.pushState(null, null, '../Test.aspx?One=numberone');
 
Share this answer
 
Comments
OriginalGriff 16-Mar-14 3:36am    
Please do not post the same thing twice: you can use the "Improve solution" widget to fix problems.
I have deleted the "unformatted" version.
Raj.rcr 16-Mar-14 3:41am    
I have not posted this again. I just posted my answer as I have solved it by myself. Check again. thanks
OriginalGriff 16-Mar-14 4:47am    
You had posted it (as Solution 2) without formatting.
You then posted it again (as Solution 3) with formatting.

It's not a problem, it's just messy, and makes some people assume you are doing it deliberately for rep points (http://www.codeproject.com/Messages/4779532/Someone-answered-his-own-question-twice.aspx) which can be dangerous for you: it's considered abuse and can lead to your account being closed. That's what brought me here, and why I deleted the "spare".

I'm sure that *you* didn't do it for any malicious purpose, hence the quick note and deletion - but we do get some members who do do it deliberately, and yes, their accounts do get squashed! For example:
http://www.codeproject.com/Messages/4779402/A-new-wrinkle-on-upvoting-your-own-answer.aspx
He no longer has an account...I don't want that to happen to you because of a simple mistake, but some members have a twitchy "report" finger! :laugh:
Raj.rcr 16-Mar-14 5:00am    
Oh. I think it was by mistake. Thanks for letting me know. :)
OriginalGriff 16-Mar-14 5:17am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900