Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 454
  • Last Modified:

Getting elements in a webpage / IE Context Menu.

Hi,
I am working on a vb6 application that collects info from a page and it utilizes a javascript to get it's values.

It is partially based on an example found online at the following URL:

http://www.freevbcode.com/ShowCode.asp?ID=4553

It uses an html file with a javescript to return values.

I am not familiar enough with javascript to properly alter this to provide the behavior I need, which is to return the images url, not the link.

Example:
With this app when an image is clicked it returns the images location which is what I want, however if the image is also a hyperlink, it returns the hyperlink.

The behavior I want is the imageurl (location) not the hyperlink it points to.

The javascipt in the html file is as follows:
<SCRIPT LANGUAGE="JavaScript">
//Gets the href for the right clicked link or image

var MyExt = new ActiveXObject("IEext.IEExtension");
if(MyExt)
      {
            var MyEvent = external.menuArguments.event;
            var MyDoc = external.menuArguments.document;

              var MySel = MyDoc.selection;
              var Rng = MySel.createRange();
              var MyStr = new String(Rng.text);
            MyExt.SelText = MyStr
            
            var ref = external.menuArguments.document.URL
            MyExt.PageURL = ref
            
            var MyElement = MyDoc.elementFromPoint(MyEvent.clientX, MyEvent.clientY);
            var MyAnchor = MyElement;
            var URL = "";
            while(MyAnchor.tagName!="A" && MyAnchor.tagName!="AREA" && MyAnchor.tagName!="HTML")
            {
                  MyAnchor=MyAnchor.parentElement;
            }
            if(MyEvent.type=="MenuExtUnknown" || MyEvent.type=="MenuExtImage" || MyEvent.type=="MenuExtAnchor")
            {
                  if(MyAnchor.tagName=="A" || MyAnchor.tagName=="AREA")
                  {
                  URL = MyAnchor.href;
                  MyExt.URL = URL;
                  MyExt.LinkType = "Link";
                  }
                  if(MyAnchor.tagName=="HTML" && MyElement.tagName=="IMG")
                  {
                  URL = MyElement.src;
                  MyExt.URL = URL;
                  MyExt.LinkType = "IMG";
                  }
            }
      
                  MyExt.ShowInfo();

      }
else
      {
            alert("An error occured!");
      }
</SCRIPT>

I hope I exaplained this clearly enough, wasn;t sure whether to post this in the VB area or Javascript area, but the VB side I get completely, it is the return from this javascript that I need to alter.

I thank you in advance.

0
flosoft
Asked:
flosoft
  • 4
  • 3
2 Solutions
 
knightEknightCommented:
if what you are saying is that you always want the image url and not the anchor href, then this might do it:

          if(MyEvent.type=="MenuExtUnknown" || MyEvent.type=="MenuExtImage" || MyEvent.type=="MenuExtAnchor")
          {
           // commenting out href stuff
           //    if(MyAnchor.tagName=="A" || MyAnchor.tagName=="AREA")
           //    {
           //    URL = MyAnchor.href;
           //    MyExt.URL = URL;
           //    MyExt.LinkType = "Link";
           //    }
           //    if(MyAnchor.tagName=="HTML" && MyElement.tagName=="IMG")
               {
               URL = MyElement.src;
               MyExt.URL = URL;
               MyExt.LinkType = "IMG";
               }
          }
0
 
flosoftAuthor Commented:
I tried this and then if the image is also part of a link
example:

<a href="someurl"><img src="/images/someimage.gif"></a>

it returns nothing at all... unless I am missing something.

what I am after is in the above case, I would want "/images/someimage.gif"

Thanks..
0
 
flosoftAuthor Commented:
Hi,
Missed a comment line, it works...some of the time that way.
For example at amazon.com if you try it on the promotion item at top left, it does not work, but it does on individual item pages??

0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
ziffgoneCommented:
Here's my solution, doesn't use ActiveX though:

<head>
<script type="text/javascript">
function findSource(e){
     var evt = e ? e : window.event;
         if(evt.target){
                  var myEvt = evt.target;
                  if(myEvt.tagName == "IMG"){
                      alert('The Images Source is: '+myEvt.src);
                  }
                  else{
                  return false;
                  }
         }
         else if(evt.srcElement){
             var myEvt = evt.srcElement;
                  // alert(myEvt.tagName);
                  if(myEvt.tagName == "IMG"){
                      alert('The Images Source is: '+myEvt.src);
                  }
                  else{
                  return false;
                  }
         }
         else{
         return null;
         }
}
    var IE = document.all?true:false;
       if(!IE){
          window.captureEvents(Event.CLICK);
          window.onmousedown = findSource;
       }
       else{
          document.onmousedown = findSource;
       }
</script>
</head>
<body>
Normal Image:<br>
<img src="http://www.experts-exchange.com/images/indexLogoPerson_06.gif" width="228" height="107"><br><br>
Hyper-Linked Image:<br>
<a href="http://www.experts-exchange.com/" onclick="return false;"><img src="http://www.experts-exchange.com/images/indexLogoPerson_02.gif" width="228" height="107" border="0"></a>

Regards...
0
 
ziffgoneCommented:
Whup, that should have said:

window.captureEvents(Event.MOUSEDOWN);

not:
window.captureEvents(Event.CLICK);

Regards...

0
 
flosoftAuthor Commented:
Adding points so I can split them. knightEknight answer too care of most of it, and through reviewing the code submitted my ziffgone, I think I wil be able to alter it a bit further to get the desired result, my thanks to both of you!
0
 
flosoftAuthor Commented:
Hope splitting was ok with both of you, and I sincerly thank you for your time!
0
 
ziffgoneCommented:
No problem flosoft, thanks for the points.

 :o)

Regards...
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now