Browser Events Handling

Discussion in 'JavaScript' started by suneel, Mar 6, 2007.

  1. #1
    Please try to understand my problem clearly.My system doesn't have
    internet connection but i have rendered search results using SOAP
    search API from other pc which is already connected
    to the Internet through some communication mechanism.Finally my system
    doesnt have internet facility.Some how i rendered search results to my
    system and popup search results in IE(intenet Explorer).Now user might
    click any one of the search results.Now my task is i need to extract
    the url which he clicked just now.Is there any another viewer which
    could display the HTML contents?Hw could i do that please suggest me?
     
    suneel, Mar 6, 2007 IP
  2. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #2
    Are you trying to get HTML content from several URLs on real time without internet communication ?
     
    ajsa52, Mar 6, 2007 IP
  3. suneel

    suneel Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Actaully i would like to know browser Event Handling.E.g I have .html file which is displayed on the IE.Now user may select one url from set of urls present in the HTML page.Hw could i know which url user has selected just now.Is this possible with java script?Please suggest me?
     
    suneel, Mar 6, 2007 IP
  4. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #4
    When you said "...which url user has selected...", are you talking about selecting urls from a list (html "select" element with "options" filled with urls), or clicking on a "href" element ?

    --> For the first option, you can add onchange, onFocus, ... events on your select. Example:

    
    HTML CODE:
    <select id="urllist" onchange="f_selected(this.selectedIndex)" onfocus="f_selected(this.selectedIndex)" >
      <option value="url1">http://www.site1.com</option>
      <option value="url2">http://www.site1.com</option>
      <option value="url3">http://www.site1.com</option>
    </select>
    
    JAVASCRIPT CODE:
    function f_selected(p_index)
    {
      var v = document.getElementsByTagName( "option" )[p_index];
      ...
    }
    
    
    Code (markup):
    --> For the second option, you can view an example on this other thread: http://forums.digitalpoint.com/showthread.php?t=187903
     
    ajsa52, Mar 6, 2007 IP
  5. suneel

    suneel Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thankx for ur reply.I have still one more doubt.I will discuss my problem with an example.
    <html>
    <body>
    <a href="http://www.yahoo.com">Yahooo</a>
    <a href="http://www.google.com">Google</a>
    <a href="http://www.nvdia.com">NVDIA</a>
    </body>
    </html>
    this is my html page with set of Hrefs.Now my task is how to retrieve the Href part like http://www.yahoo.com whenever user clicks yahoo.And same for remaing hrefs.Is there any java script to achieve this?Please give me reply.

    thanking you inadvance.
     
    suneel, Mar 10, 2007 IP
  6. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Try this. It will alert the href.
    function chgLinks() {
    	for(lnk in document.links){
    		lnk = document.links[lnk];
    		lnk.onclick = function(){
    				alert(lnk.href)
    			};
    	}
    }
    document.onload = chgLinks;
    
    Code (markup):
     
    Aragorn, Mar 10, 2007 IP
  7. suneel

    suneel Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thankx for ur reply.Following is my code.Is there any changes that i have to do inorder to fulfill my task.Please note i am new to java script.

    <html>
    <head>
    <script LANGUAGE="JavaScript">
    function chgLinks() {
    for(lnk in document.links){
    lnk = document.links[lnk];
    lnk.onclick = function(){
    alert(lnk.href)
    };
    }
    }
    document.onload = chgLinks;
    </script>
    </head>
    <body>
    <a href="http://www.google.com" >Google</a>
    <a href="http://www.yahoo.com" >Yahoo</a>
    <a href="http://www.nvdia.com" >NVDIA</a>
    </body>
    </html>

    thanking you inadvance.
     
    suneel, Mar 11, 2007 IP
  8. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #8
    The code above does not work.
    Try this:
    
    <html>
    <head />
    
    <body onload="chgLinks()">
    
    <script type="text/javascript">
    
    function myalert() { alert( this.href ); } 
    
    function chgLinks() 
    {
      var a = document.getElementsByTagName("a");
      for(i=0; i<a.length; i++)
      {
        a[i].onclick = myalert;
      }	
    }
    
    </script>
    
    <a href="http://www.google.com" >Google</a>
    <a href="http://www.yahoo.com" >Yahoo</a>
    <a href="http://www.nvdia.com" >NVDIA</a>
    
    </body>
    </html>
    
    Code (markup):
    This code is a simplified version taken from the code on this thread: "counting clicks with static links".
     
    ajsa52, Mar 11, 2007 IP
  9. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #9
    I am sorry. I took it from a web directory I created long back. Made some modification to it quickly and didn't test it. Here is the modified code.
    
    function chgLinks() {
    	for(lnk in document.links){
    		lnk = document.links[lnk];
    		lnk.onclick = function(){
    			[COLOR="Red"]alert(this.href);
    			return false;[/COLOR]
    		};
    	}
    }
    window.onload = chgLinks;
    
    Code (markup):
    Ofcourse, see the part I have marked in red. Presently what it does is show a msg box with the link url. Change it with what you want to do. The url is available in this.href. If you use the return false statement, then the page will not redirect to the link url. See the difference by clicking on the link with and without the return false statement.
     
    Aragorn, Mar 11, 2007 IP
  10. suneel

    suneel Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    thanking you very much for ur reply.It's working.Now i have one more doubt.Is there any way inorder to communicate from java script to java application.
    <html>
    <head />
    <body onload="chgLinks()">
    <script type="text/javascript">

    function chgLinks() {
    for(lnk in document.links){
    lnk = document.links[lnk];
    lnk.onclick = function(){
    alert(this.href);
    return false;
    };
    }
    }
    </script>
    <a href="http://www.google.com" >Google</a>
    <a href="http://www.yahoo.com" >Yahoo</a>
    <a href="http://www.nvdia.com" >NVDIA</a>

    </body>
    </html>
    in this code how could i pass "this.href" java script var to java variable.

    thanking you inadvance.
     
    suneel, Mar 12, 2007 IP
  11. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #11
    Aragorn, Mar 14, 2007 IP