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.

2
  • document.getElementById('http:\www.somewebsite.com') makes no sense. please explain what you think this is doing. Commented Apr 7, 2011 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? Commented Apr 8, 2011 at 1:30

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

3 Comments

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?
he answered your question. you're passing the open() method a DOM element not a string.
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.
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

1 Comment

I got the help from this: stackoverflow.com/questions/553813/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.