Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Scripting / March 2007

Tip: Looking for answers? Try searching our database.

DOM Node Creation issue

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John H Clark - 08 Mar 2007 19:39 GMT
During the initialization of a page I use the following script to create an
image then append the image to an existing div element.

       var theImage = new Image();
       theImage.id = "anId";
       theImage.src = "gallery/image.jpg";
       var mouseAttribute = document.createAttribute("onclick");
       mouseAttribute.value = "javascript:displayImage(this.id)";
       theImage.setAttributeNode(mouseAttribute);
       var existingDiv = document.getElementById("existingDiv");
       existingDiv.appendChild(theImage);

There is a function named "displayImage";

This results in an img element being created and placed on the page as
expected.  However, when the image is clicked there is no event fired in IE
7.  However, in Firefox an event is fired and the response is as expected.

Why is the event not fired in IE 7?  Any suggestions on how to make this
work in IE 7?

Thanks for your help.

Signature

John H Clark
www.weownit.coop

Ayush - 08 Mar 2007 22:15 GMT
John H Clark wrote ::
> During the initialization of a page I use the following script to create an
> image then append the image to an existing div element.
[quoted text clipped - 3 lines]
>         theImage.src = "gallery/image.jpg";
>         var mouseAttribute = document.createAttribute("onclick");

use attachEvent (for IE) and addEventListener (for other browsers) to add
the event.

>         mouseAttribute.value = "javascript:displayImage(this.id)";

dont use javascript protocol here. it will not work. use :
displayImage(this.id)

>         theImage.setAttributeNode(mouseAttribute);
>         var existingDiv = document.getElementById("existingDiv");
>         existingDiv.appendChild(theImage);

----
        var theImage = new Image();
        theImage.id = "anId";
        theImage.src = "gallery/image.jpg";
    theImage.onclick=function(){displayImage(this.id)}   // this will work on
all browsers
        var existingDiv = document.getElementById("existingDiv");
        existingDiv.appendChild(theImage);
----
or
----
        var theImage = new Image();
        theImage.id = "anId";
        theImage.src = "gallery/image.jpg";
    if(thImage.attachEvent)
      theImage.attachEvent("onclick",function(){displayImage(this.id)})
    else if(theImage.addEvenetListener){
    theImahe.addEventListener("click",function(){displayImage(this.id)},false)
        var existingDiv = document.getElementById("existingDiv");
        existingDiv.appendChild(theImage);
----

> However, in Firefox an event is fired and the response is as expected.

Really !!

Good Luck, Ayush.
Signature

Setup Outlook Express to read MS Newsgroups :
http://www.microsoft.com/windows/ie/support/newsgroups/howto.mspx


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.