I am using Javascript to open a URL in a new window. It works fine except that the calling window refreshes and moves back to the top of the page.
How do I make sure that the calling page stays in the same position?
Printable View
I am using Javascript to open a URL in a new window. It works fine except that the calling window refreshes and moves back to the top of the page.
How do I make sure that the calling page stays in the same position?
Hi,
Can u please paste the code .
It will help to understand.
Regards
-Sachien
VB Code:
Private Sub JavaScriptOpenWindow(ByRef sURL As String, Optional ByRef sHeight As String = "768", Optional ByRef sWidth As String = "1024", Optional ByRef sToolbar As String = "0", _ Optional ByRef sScrollBars As String = "0", Optional ByRef sLocation As String = "0", Optional ByRef sStatusBar As String = "0", _ Optional ByRef sMenuBar As String = "0", Optional ByRef sResizable As String = "0", _ Optional ByRef sLeft As String = "0", Optional ByRef sRight As String = "0") Dim sWindowRef As String = GenRandom() Dim sJavaScript As String = "<script language=javascript>window.open('" & sURL & "','" & sWindowRef & "', 'toolbar=" & sToolbar & ",scrollbars=" & sScrollBars & ",location=" & sLocation & ",statusbar=" & sStatusBar & ",menubar=" & sMenuBar & ",resizable=" & sResizable & ",left=" & sLeft & ",right=" & sRight & ",width=" & sWidth & ",height=" & sHeight & "');</script>" Response.Write(sJavaScript) Response.Flush() End Sub
The function GenRandom returns a random string which uniquely identifies the new window so that subsequent calls open another new window.
What events on the page make you call a this subroutine? Is it being called from a link?
I am calling it from a DataGrid1_ItemCommand routine in response to clicking on a link button column in the grid.
Perhaps you should be setting the navigateurl for your hyperlinks in the itemdatabound event, surely there is some logic to the sURL you generate? Simply make it call a predefined javascript function which already exists in your ASPX code instead of going through this hassle.
If I can find a way around this with my current code, it would be better because it works exactly the way I want it to except for the refresh problem.
Is there some way that I can create an anchor at the row on the grid that the user clicked on to open the new window? I could store the row number in either a hidden label or in a session variable and have a button at the top of the page called "Go To Last Position" which when clicked on would go the row that was clicked previously. This would do the job for me.
Great Code Robertx , you have gained my rating for this ;)