0

The following is on my asp.net page written in c#. Where someURL is asp.net string variable that contained a html link such as http:\www.somewebsite.com.

ClientScript.RegisterStartupScript(typeof(Page), "openPage", "<script type='text/JavaScript'>var link1 = document.getElementById('<%=someURL.ClientID%>');window.open(link1);</script>");

The objective is when the above javascript executed it will open a new browser page with the url contained inside the someURL string (http:\www.somewebsite.com, etc.)

My problem was that it opens a blank page with nothing in the url. And I think I know why... the someURL variable referenced inside the javascript code was not from the calling asp.net page.

Am I right? Any suggestion on how to achieve my objective?

Thank you.

user523234
  • 14,323
  • 10
  • 62
  • 102
  • document.getElementById('http:\www.somewebsite.com') makes no sense. please explain what you think this is doing. – herostwist Apr 07 '11 at 20:58
  • Now you pointed it out. That won't do what I think it would. But my original objective is to replace the asp.net's Response.Redirect(someURL) with javascript that will do the same but also open a new browser window instead of the same window as the asp.net's Response.Redirect does. Any suggestion? – user523234 Apr 08 '11 at 01:30

2 Answers2

1

Not sure of the purpose of this. But the variable link1 is a reference to a DOM element.

you need to get the text (depending on what type of control someURL is in your ASP page) value of that DOM element in order to pass it to the other script.

This is by no means elegant or recommended but will work

C# - in your codebehind page

public string nextURL = "http://yourdomain.com/yourpage.aspx"

ASP.NET - in your aspx page

<script type="text/javascript">

  var myLink = '<%= nextURL %>';

  function newPage() {
    window.open(myLink);
  }

</script>
Bobby Borszich
  • 11,639
  • 9
  • 37
  • 35
  • My original objective is to replace the asp.net's Response.Redirect(someURL) with javascript that will do the same but also open a new browser window instead of the same window as the asp.net's Response.Redirect does. Any suggestion? – user523234 Apr 08 '11 at 01:33
  • he answered your question. you're passing the open() method a DOM element not a string. – Rush Frisby Apr 08 '11 at 01:57
  • While I was aware of the concept behind this. I was looking for some specific syntax help. See my other comment for my final coding. – user523234 Apr 08 '11 at 17:26
0

For the benefit of those new learners, after a bit of struggle here is my final code that will do the job for me:

In my calling .cs file:

   protected String someURL;  //global variable

    ClientScript.RegisterStartupScript(typeof(Page), "openPage", "<script type='text/JavaScript'>window.open(currentLinkURL);</script>");

In my .aspx file:

var currentLinkURL = '<% = someURL %>';  //currentLinkURL is a javascript variable
user523234
  • 14,323
  • 10
  • 62
  • 102
  • I got the help from this: http://stackoverflow.com/questions/553813/how-do-i-give-javascript-variables-data-from-asp-net-variables – user523234 Apr 08 '11 at 17:34