473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Javascript in user control

15
Hello

I have a user control which is referenced in my other pages, when clicked should open the specified URL in new window. How can i achieve this?

I have to pass uid as well

http://abc.com/Home.aspx?uid=Session("uid")

I am trying the following

<a href="javascript:newWin=window.open('http://abc.com/Home.aspx?uid=\"+Session("uid")+"','','resizable=y es,width=567,height=300,scrollbars');"><img border="0" alt="" src="http://bytes.com/submit/images/btn1.jpg" /></a>

Please advise
Sep 2 '09 #1
9 6556
try this

<img onclick="window.open('myurl');" src="myimg" onmouseover="this.style.cursor='pointer';" />
Sep 2 '09 #2
renuami
15
Thank you for the quick response.

This will work fine if i write the below two ways.

<img onclick="window.open(''http://abc.com/Home.aspx');" src="myimg" onmouseover="this.style.cursor='pointer';" />

This works but doing this means we are hard coding the uid value..
<img onclick="window.open(''http://abc.com/Home.aspx?uid=1234');" src="myimg" onmouseover="this.style.cursor='pointer';" />

uid i am getting from session variable. I should pass the userid

Any more ideas? Please advise

I tried the below but still not working

<img onclick="window.open(' http://abc.com/Home.aspx?uid=' +Session("uid").ToString() );" src="myimg" onmouseover="this.style.cursor='pointer';" />

<img onclick="window.open(' http://abc.com/Home.aspx?uid=' +<%=Session("uid")%>);" src="myimg" onmouseover="this.style.cursor='pointer';" />
Sep 2 '09 #3
Frinavale
9,735 Expert Mod 8TB
Passing the UID in the URL (query string) or using Session is completely up to you.

If you already have the UID in Session then I would suggest retrieving it from Session instead of passing it through the query string...there's no need to pass it through query string in this case.
Sep 2 '09 #4
renuami
15
Yes i can retrieve it from session if it is within the same project/website.

But that is not the case with my issue.

I am working in one website http:// xyz.com
from this i am trying to open another website http:// abc.com and pass the Uid for that new website.

let me know if there are any other ways how we can pass parameters through the querystring to a totally new website/project
Sep 3 '09 #5
Frinavale
9,735 Expert Mod 8TB
Oh, sorry. I miss read your question. I thought you were asking which method of passing the UID was better. I didn't realize that your JavaScript wasn't working.


Your code should look like this:
Expand|Select|Wrap|Line Numbers
  1. <img onclick="window.open(' http://abc.com/Home.aspx?uid=" + Session("uid").ToString() + "); src="myimg" onmouseover="this.style.cursor='pointer';" />
Sep 3 '09 #6
renuami
15
Thank you.

But still no luck...

Is this syntax correct? Am i missing something???

Error is Attribute uid is not valid attribute of element img (see in bold below). My session variable is correct.


Expand|Select|Wrap|Line Numbers
  1. <img alt="" onclick="window.open('http://abc.com/Home.aspx?uid='+ Session("uid").ToString())"; src="../myimg.jpg" onmouseover="this.style.cursor='pointer';" /> 
Sep 3 '09 #7
Frinavale
9,735 Expert Mod 8TB
Yes there's a syntax error. You have a ' instead of a " and your missing a + sign...

Please be aware that you cannot access Session variables from JavaScript.
Therefore what you should be doing is creating a String containing the JavaScript that should be executed when the image is clicked.

The JavaScript that needs to be executed is:
Expand|Select|Wrap|Line Numbers
  1. window.open('http://abc.com/Home.aspx?uid=12345');
  2.  
This will open the Home.aspx page in the abc website in a new window...passing it uid=12345.

The thing is that 12345 is stored in Session on the server. That means that when you create the string containing the JavaScript you need to add the value that is stored in session:
Expand|Select|Wrap|Line Numbers
  1. Public ReadOnly Property ImageOnclickJavaScriptString() As String
  2.   Get
  3.     Dim javaScriptString as String 
  4.     javaScriptString ="window.open('http://abc.com/Home.aspx?uid="
  5.     javaScriptString = javaScriptString + Session("uid")
  6.     javaScriptString = javaScriptString + "');"
  7.     return javaScriptString
  8.   End Get
  9. End Property
  10.  
Note that I put this into a Public Property. I could have put it into a Protected Property...but the important thing is that this property is accessible to the ASP code.

Now in your ASP code you can call the Response.Write() method to write this string into the HTML <img ...> tag. The ASP short and for calling the Response.Write method is: <%= %>

So, in your ASP code you'll have:
Expand|Select|Wrap|Line Numbers
  1. <img alt="" onclick="<%= Me.ImageOnclickJavaScriptString %>"; src="../myimg.jpg" onmouseover="this.style.cursor='pointer';" /> 
(All this time I've been assuming you're using VB.NET...if you're using C# you need to modify everything to use that syntax instead)

To make things a lot easier for yourself it would be a good idea to use an ASP.NET Image control instead of an HTML <img>. Image controls are accessible on the server and it makes things a little easier...

If you change your <img> tag you'd have:

Expand|Select|Wrap|Line Numbers
  1. <asp:Image id="myImg" runat="server" ImageURL="~/myimg.jpg" />

Now in your server code you could just simply set the onclick event of the Image like so:

Expand|Select|Wrap|Line Numbers
  1.    Dim javaScriptString as String 
  2.    javaScriptString ="window.open('http://abc.com/Home.aspx?uid="
  3.    javaScriptString = javaScriptString + Session("uid")
  4.    javaScriptString = javaScriptString + "');"
  5.  
  6.    myImg.Attributes.Add("onclick",javaScriptString)
  7.    myImg.Attributes.Add("onmouseover","this.style.cursor='pointer';")
  8.  
You'd place this in your Page Load event when IsPostback = False...or in your Page PreRender event if you want....anywhere really, so long as it's done the first time the page is loaded. You only have to do it the first time the page loads because the ASP.NET Image control remembers this information between postbacks (whereas the HTML <img> tag does not).

-Frinny
Sep 3 '09 #8
renuami
15
Frinny

Thanks for the detail explanation . That did the trick.

Thanks
again
Sep 3 '09 #9
Frinavale
9,735 Expert Mod 8TB
After that whole explanation I realized that it could be done using much less code (the explanation above really outlined what was going on though).

You could have just called the Response.Write method to write the Session variable into the string. Just realize that any code in the asp tags <% %> is executed on the server and that <%= %> is short hand for <%Response.Write("someString") %>

Expand|Select|Wrap|Line Numbers
  1. <img alt="" onclick="window.open('http://abc.com/Home.aspx?uid=<%=Session("uid").ToString() %>);" src="../myimg.jpg" onmouseover="this.style.cursor='pointer';" /> 
This way you don't have to write the Property just to return a string...

-Frinny
Sep 3 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: New User | last post by:
I have a System.Web.UI.UserControl as custom control and I have a javascript block for user control. The problem is I want to bring src attribute from outside as property or other method. e.g...
8
by: rn5a | last post by:
I have gone through a no. of posts in this NewsGroup regarding my problem but alas, couldn't come across one which would have helped me in resolving the issue. My problem is this: An ASPX Form...
4
by: archana | last post by:
Hi all, i am having one user control. what i want is to add javascript which will gets called on button click of user control. but user control is not working if i add javascript in user...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.