Wrox Programmer Forums
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 9th, 2007, 02:41 PM
Registered User
 
Join Date: Sep 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default embeding javascript

Hi everybody,

I have a javascript code that works well in html but I could not make it work in aspx file.

I have registered the javascript function first:

    Protected Sub Page_Load(.....) Handles Me.Load
        Me.addScriptToPage()
    End Sub

    Private Sub addScriptToPage()
        Dim script As String = "<script language='JavaScript'>"
        script &= "function changeImage(filename){"
        script &= "document.mainimage.src = filename;}"
        script &= "</script>"

        If Not (Me.IsClientScriptBlockRegistered("scriptBlok")) Then
            Me.RegisterClientScriptBlock("scriptBlok", script)
        End If
    End Sub

Then I called this function in this way:

Response.Write("<a href=javascript:changeImage(" & "'C:\matilda01.jpg'" & ")>MyImage</a>")

There is a image on the page called "mainimage" on which other images will be displayed by using changeImage function.. But it did not work..

Have you noticed any missing points???

 
Old September 9th, 2007, 07:39 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I can't say I see anything wrong with the javascript itself, however there is one major problem which might not be immediately apparent:

You are providing an explicit *local* file page for an image. This will only work on your machine. Any other user browsing your site won't see "C:\matilda01.jpg" unless they happen to have a copy of it on their local PC.

-Peter
 
Old September 10th, 2007, 01:31 AM
Registered User
 
Join Date: Sep 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Response.Write("<a href=javascript:changeImage(" & "'C:\matilda01.jpg'" & ")>MyImage</a>")

I think there could be problem with this line.
Do you think that is the syntax correct in the code line above?


 
Old September 10th, 2007, 10:38 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Generally, you should try to enclose attribute values in quotes. Is the image going to be hard coded on this line? If so you are performing an unnecessary concatenation.

Response.Write("<a href=""javascript:changeImage('C:\matilda01.jpg')" ">MyImage</a>")

Note the two double quotes. That escapes the " character in VB.Net.

-Peter
 
Old September 10th, 2007, 01:50 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

In addition to what Peter said, you may want to stay clear from using Response.Write.

Depending on where you call it, the output of Response.Write may end up outside (above) the opening HTML tag, as the page hasn't begun outputting HTML yet.

Instead, add a Literal to your page and assign the text / script to the Literal's Text property.

Cheers,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004





Similar Threads
Thread Thread Starter Forum Replies Last Post
JavaScript. Magxtopher Javascript 2 April 1st, 2014 09:27 PM
stop current javascript function using javascript sakthi Javascript 3 June 2nd, 2008 03:30 PM
embeding an OLE object ricespn Beginning VB 6 0 August 17th, 2007 12:47 AM
Javascript help? Sidearm Other Programming Languages 0 April 13th, 2006 08:32 AM
"text/javascript" and "javascript" rupen Javascript 1 June 24th, 2005 07:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.