SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jan 2004
    Location
    UK
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question problem with .click method of <a> tag

    I have programmed a web page in jscript to find all <a> tags on the page and list the innerHTML in a <select> box. Clicking on an item in the list should execute the href link as if the link itself were clicked on (some links have the "Target" attribute set which must be taken care of). To do this I set a pointer to the relevant <a> tag and use the click method as below. The first link selected launches OK, but all subsequent selections launch the same page, even though SelectedAnchor.href has changed.

    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE></TITLE>

    <script type="text/javascript">

    var allAnchors;

    function init()
    {
    allAnchors = document.all.tags("a");

    var theSelect = document.getElementById("menuSelect");

    for(var i = 0; i < allAnchors.length; i++)
    {
    currAnchor = allAnchors[i];
    currText = currAnchor.innerHTML;
    theSelect.options.add(document.createElement("OPTION"));
    theSelect.options[i].innertext = currText;
    theSelect.options[i].label = i;
    debugbox.value = currText;
    }
    }

    window.onload = init;

    // jump menu function and event handler

    function doJump(select)
    {
    selected = select.selectedIndex;
    newLoc = select.options[selected].label;
    ThisLink = allAnchors[newLoc];
    debugbox.value = newLoc & select.options[selected].innertext;
    ThisLink.click;
    }
    </script>

    </head>

    <body onload = "init();">
    <INPUT id=debugbox></INPUT>
    <select id="menuSelect" onchange="doJump(this);">
    </select>
    <p>
    <a href="file://D:\My Documents\EditRomLinksR.htm" target=Right>EditRomLinksR</a>
    <p>
    <a href="file://D:\My Documents\CommonDesktop.htm" target=Right>CommonDesktop</a>
    <p>
    <a href="file://D:\My Documents\UserNameAdmin.htm" target=Right>UserNameAdmin</a>
    </body>
    </html>where ListItem is the index of the required anchor on the page and AllAnchors is a colllection of all the anchors.

    I first scripted this in VBScript, but have attempted to convert to JScript.
    The above script does not populate the options properly for dome reason.

    When done in VBScript, the first link selected launches OK, but all subsequent selections launch the same page, even though SelectedAnchor.href has changed.
    I have tried using window.location.href with the same results.
    I have run out of ideas.I need to run this code for dozens of pages with hundreds of links which do not have identifiers or names and which also have Targets set, e.g. target = right.
    All my links are all to files elsewhere on the server.

    Stephen Dolley

  2. #2
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    instead of
    Code:
    currText = currAnchor.innerHTML;
    try
    Code:
    currText=currAnchor.firstChild.nodeValue
    instead, don't know if it will help in your case but its more browser friendly.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  3. #3
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you post the hole chunk of code? I miss the options on your select tag and don't get either what the meaning of the empty input tag is ... ??

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Some errors...innerText, multiple onload assignments, calling the click() method - not a good approach to begin with - without the operator pair (parentheses), as well as a mix of W3C DOM and Internet Explorer-specific references. Try this:
    Code:
    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE></TITLE>
    
    <script type="text/javascript">
    
    var allLinks = document.getElementsByTagName('A');
    
    onload = function()
    {
    	var currLink, currText, newOpt, theSelect = document.getElementById('menuSelect');
    	for (var i = 0; i < allLinks.length; i++)
    	{
    		currLink = allLinks.item(i);
    		if (!currLink.href) //named anchor
    			continue;
    		currText = currLink.innerHTML;
    		newOpt = new Option(currText, currLink.href);
    		theSelect[theSelect.length] = newOpt;
    	}
    }
    
    function doJump(oSelect)
    {
    	var href;
    	if (href = oSelect.options[oSelect.selectedIndex].value)
    		location = href;
    	oSelect.form.debugbox.value = oSelect.options[oSelect.selectedIndex].text;
    }
    
    </script>
    </head>
    <body>
    <form>
    <input name="debugbox"></input>
    <select id="menuSelect" onchange="return doJump(this)">
    <option selected="selected" value="">- choose -</option>
    </select>
    </form>
    <p>
    <a href="&#106;avascrip&#116;&#58;alert('EditRomLinksR')" target="Right">EditRomLinksR</a>
    <p>
    <a href="&#106;avascrip&#116;&#58;alert('CommonDesktop')" target="Right">CommonDesktop</a>
    <p>
    <a href="&#106;avascrip&#116;&#58;alert('UserNameAdmin')" target="Right">UserNameAdmin</a>
    </body>
    </html>
    ::: certified wild guess :::

  5. #5
    SitePoint Member
    Join Date
    Jan 2004
    Location
    UK
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help with this.

    My list is now populated properly, and the links work, but I need to handle the target attribute. As it is, the links open in the current window, but they need to open in the window indicated by the target attribute.
    That is why I was attempting to use the .click method.

    Any ideas?

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    location = href;

    'location' (could also use location.href) refers to the Location object found at window.location, used to set new urls. Used with 'window' (or nothing, as 'window' is implied) it references the window the script runs in. Just grab the window object of your choice:

    top.Right.location = href; // wild guess, simple frameset, frame named 'Right'
    ::: certified wild guess :::


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •