Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 9th, 2007, 02:41 PM
Registered User
 
Join Date: Sep 2007
Location: Ankara, , Turkey.
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???

  #2 (permalink)  
Old September 9th, 2007, 07:39 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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
  #3 (permalink)  
Old September 10th, 2007, 01:31 AM
Registered User
 
Join Date: Sep 2007
Location: Ankara, , Turkey.
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?


  #4 (permalink)  
Old September 10th, 2007, 10:38 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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
  #5 (permalink)  
Old September 10th, 2007, 01:50 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:19 PM.


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