Javascript in user control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • renuami
    New Member
    • Feb 2009
    • 15

    Javascript in user control

    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=S ession("uid")

    I am trying the following

    <a href="javascrip t:newWin=window .open('http://abc.com/Home.aspx?uid=\ "+Session("uid" )+"','','resiza ble=yes,width=5 67,height=300,s crollbars');">< img border="0" alt="" src="http://bytes.com/submit/images/btn1.jpg" /></a>

    Please advise
  • BiffMaGriff
    New Member
    • Aug 2007
    • 39

    #2
    try this

    <img onclick="window .open('myurl'); " src="myimg" onmouseover="th is.style.cursor ='pointer';" />

    Comment

    • renuami
      New Member
      • Feb 2009
      • 15

      #3
      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="th is.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=1 234');" src="myimg" onmouseover="th is.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="th is.style.cursor ='pointer';" />

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

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        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.

        Comment

        • renuami
          New Member
          • Feb 2009
          • 15

          #5
          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

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            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:
            Code:
            <img onclick="window.open(' http://abc.com/Home.aspx?uid=" + Session("uid").ToString() + "); src="myimg" onmouseover="this.style.cursor='pointer';" />

            Comment

            • renuami
              New Member
              • Feb 2009
              • 15

              #7
              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.


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

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                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:
                Code:
                window.open('http://abc.com/Home.aspx?uid=12345');
                This will open the Home.aspx page in the abc website in a new window...passin g 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:
                Code:
                Public ReadOnly Property ImageOnclickJavaScriptString() As String
                  Get
                    Dim javaScriptString as String 
                    javaScriptString ="window.open('http://abc.com/Home.aspx?uid="
                    javaScriptString = javaScriptString + Session("uid")
                    javaScriptString = javaScriptString + "');"
                    return javaScriptString
                  End Get
                End Property
                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:
                Code:
                <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:

                Code:
                <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:

                Code:
                   Dim javaScriptString as String 
                   javaScriptString ="window.open('http://abc.com/Home.aspx?uid="
                   javaScriptString = javaScriptString + Session("uid")
                   javaScriptString = javaScriptString + "');"
                  
                   myImg.Attributes.Add("onclick",javaScriptString)
                   myImg.Attributes.Add("onmouseover","this.style.cursor='pointer';")
                You'd place this in your Page Load event when IsPostback = False...or in your Page PreRender event if you want....anywher e 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

                Comment

                • renuami
                  New Member
                  • Feb 2009
                  • 15

                  #9
                  Frinny

                  Thanks for the detail explanation . That did the trick.

                  Thanks
                  again

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    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.Writ e("someString ") %>

                    Code:
                    <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

                    Comment

                    Working...