JavaScript - Variable To Be Used In Href
Please check out the code and tell me how can i use the variable as a href link.
var rand_link = new Array (); rand_link[0]="asdhrfe22k"; rand_link[1]="iqh3wjhs8s"; rand_link[2]="snbds7s8h3"; rand_link[3]="=jh99shhs99"; rand_link[4]="shdbfh3h83"; var rand = Math.floor(5 * Math.random()); var ref = rand_link[rand]; document.write("<a href=http://www.youtube.com/watch?v= [with the variabel "ref"]>Click here</a>"); where, the array of links are in rand_link. Similar TutorialsHi, I am trying to write some code (not my main function in my job) to have a variable be inserted in the middle of a "<a href>" tag. I created an XML file and I am parsing it with XSL and that is working well. In my XSL code I have the following (and it is working): Code: <xsl:for-each select="documents/data[frequency='Daily']"> <script>var meetingIndicator = <xsl:value-of select="meetingIndicator"/>; </script> <tr> <td class="tableLink"><a href="" onMouseover="showmenu(event,linkset[0], '450px')" onMouseout="delayhidemenu()"><xsl:value-of select="meetingCategory"/></a><script>document.write(meetingIndicator);</script> </td> <td><xsl:value-of select="meetingCategory"/></td> <td> <xsl:value-of select="duration"/> </td> <td> <xsl:value-of select="purpose"/> </td> <td> <xsl:value-of select="idealFor"/> </td> <td> <xsl:value-of select="instructions"/> </td> <td class="tableLink"> <a><xsl:attribute name="href"> <xsl:value-of select="documentURL"/></xsl:attribute> <xsl:value-of select="supportingDocuments"/></a> </td> </tr> </xsl:for-each> </table> In this part of the code: Code: <td class="tableLink"><a href="" onMouseover="showmenu(event,linkset[0], '450px')" onMouseout="delayhidemenu()"><xsl:value-of select="meetingCategory"/></a><script>document.write(meetingIndicator);</script> I want to replace the 0 in (event,linkset[0], '450px') with the "meetingIndicator" variable (or even the "<xsl:value-of select="meetingIndicator"/>;") but I don't know how to. I can call the "<xsl:value-of select="meetingIndicator"/>;" and it displays the correct value from the XML file. i just need to know how to place that in the "event,linkset[0]" part of the "a href" tag. I guess it is going to be pretty easy but I have been googling and playing it since yesterday and i am not having any luck. Thank you. Hi, I'm using the following Javascript code to show a div layer depending on the URL Code: <script type="text/javascript"> if (location.href.match(/folder/)) { document.getElementById("div-layer").style.display="block"; } </script> This works perfectly when I visit http://www.domain.com/folder but I do not want it to match any subfolders beneath /folder, for example: http://www.domain.com/folder/sub-folder http://www.domain.com/folder/sub-folder2 etc. Does anyone know how I can modify the code to only match /folder or /folder/ and no subfolders beneath it? Thank you in advance. We have a function that validates a form and then does submit (and a bunch of other stuff) We called it using: Code: <td class="MyButtonStyle"> <a href="#" OnClick="MyFunction();" title="Save changes and continue">SAVE</a> </td> This worked fine on the browsers we were using (IE6 in the main), but we now find doesn't work on IE5.01, Mac and some others. OK, so having done some research I now realise I needed RETURN FALSE in OnClick. So ... faced with having to change a lot of pages I'd like to have the best possible syntax (by which I suppose I rellay mean most-browser-compatible), and I've seen a number of options; advice on picking a good'un would be much appreciated. Amongst the stuff I've seen is: Code: 1. href="#" onclick="MyFunction();return false;" 2. href="#null" onclick="MyFunction();return false;" 3. href="NoJavaScriptAbility.htm" onclick="MyFunction();return false;" 4. href="#" onclick="return MyFunction();" 5. href="javascript:;" onclick="return MyFunction();" 6. href="javascript://" onclick="return MyFunction();" 7. href="javascript:void(0);" onclick="return MyFunction();" 8. href="javascript:MyFunction();" What about what shows in the Status bar? Should I have an OnMouseOver event too to describe what MyFunction is going to do? Rather than displaying a meaningless link. Anything else to note? (This function is part of a library being reused for major web site development over the next however many years, and not for single-project build-and-throw-away, so all possible functionality, including The Kitchen Sink!, is up for consideration). Thanks Kristen I have to rework a script now, and I'm wondering how to change the script below to find the text inside of a href tag that is nested inside div class "posted-by". I want the result "Name Surname" here's what it's searching in: Code: <span class="posted-by"> <a href="/from_the_heart/author/namesurname"> <img class="inline-icon user-registered-icon" src="/universal/images/transparent.png" alt="Author" title="Author"> Name Surname </a> </span> And here's the existing code. I got some help on codingforums to create the script below that strips down to the 'a href' but now I need the text inside (because there's a space between the first and last name) Code: function doit() { if($('div').hasClass('single-journal-entry-wrapper')) { var theURL = null; var spans = document.getElementsByTagName("span"); for ( var s = 0; s < spans.length; s++ ) { // searching through spans for the class name: var span = spans[s]; if ( span.className.indexOf("posted-by") >= 0 ) { var useit = span.getElementsByTagName("a")[0]; theURL = useit.href; break; // Here things deviate.I need the text inside the href } } if ( theURL == null ) { alert("failed!"); } else { var parts = theURL.split('/'); var result = parts[parts.length-1]; if (result == "christopheryen") { var txt=document.getElementById("author"); txt.innerHTML="adsfasdfasdf"; } else if (result == "juliapatrick") {etc.} Any ideas why Code: <a href="#" title="" onClick="alert(this.parentNode.childNodes[0].innerHTML);"> works but Code: <a href="javascript:alert(this.parentNode.childNodes[0].innerHTML);"> doesn't? In the second case, my error console says this.parentNode is undefined. Ok so i'm trying to write a Greasemonkey script to change all the hrefs on a single page. The href by default looks like this: Code: <a href="javascript:get('246154895')" class="postid">ID</a> What i'm trying to do is make the number from get() appear in stead of "ID". How should i get this done? I started up with this: Code: var posts = document.getElementsByClassname('postid'); for (i=0; i<posts.length; i++) { //Replacing } But i doubt it will work, since there are other items with the class "postid" that aren't related to these tags i'm trying to change. P.S. I'm new to JS so yeah :P Hello, I am needing help in copying a url to another href on a page. Here is the scenario: I have a page with multiple <LI>. Within each <LI> there is A hidden link An image A visible link This is the code within each <LI>: Code: <h2 class="postTitle" style="display: none;"><a href="URL to copy">LI Name</a></h2> <a href="Copied URL"><img alt="" src="LI Image" /></a> <a href="Copied URL">LI Name</a> Each <LI> will contain a different image, LI name, and link. I am needing a way to get the hidden URL for each LI copied into the visible href. Hope this makes sense. Thanks in advance for your help. At first : Peace of code i have now: Code: <a onclick="doRate('-1', '{$id}'); return false;"><img src="{THEME}/dleimages/down.gif" title="Blogai(-)" alt="Blogai(-)" class="r2-unit" width="16" height="16" align="absmiddle" /></a> So i thinking that if user has JavaScript in his browser disabled it won't work. So gonna change it to this : like i saw in other webs: Code: <a onclick="doRate('-1', '{$id}'); return false;" href="llink.html"><img src="{THEME}/dleimages/down.gif" title="Blogai(-)" alt="Blogai(-)" class="r2-unit" width="16" height="16" align="absmiddle" /></a> But i'm afraid that if user WILL have JS support enabled, href tag will be executed anyways. Any ideas? if f returns true then href gets 'executed', else not. Works in FF, GC. In IE 7 does not work. How to get this working in IE ? Code: <a onclick="return f();" href="../jsp/..."> Code: function f() { return true; } Hi everyone, I am looking for JS script to replace find all the href values on a page and replace those href values matching them with an array of values. my array, lets say is a = {'some.ex.com', 'abc.ex.com/tv', 'some.name.ex.com', 'bcd.ex.com/dir1/dir2/some.jsp' }; etc i want to find all the URLs on a given page, check if each found url matches with any of the values in the array, if match found, just replace only "ex.com" with "example.com" for every match. I am all new to JS and please please help Many thanks I have the index page which contains the menus with hrefs. Now i have to generate the hrefs dynamically and the problem is the menus are created by the plugin so i can edit that file So i was thinking of loading the new url in hidden elemenst and when the page loads then chnage the hrefs of those menus. I want to ask that where on the page should i display the hidden elements so that when the page loads then by using JS i can chnage the href of few menu items I was thinking of doing that on the top of page but that does not look nice to have any element before body Hi, I am trying to add one image in footer of my application. Code: <div class="rightLinks"> <a href="#"><script src=https://seal.somesign.com/getseal?host_name=www.validsite.com&size=S&use_flash=NO&use_transparent=NO&lang=en></script></a> </div> But this is not working. Please let me know, what is wrong in it. And how can we correct it. Thanks!! Regards, Neha Hi Chaps, I have thumbnail gallery which, when clicked opens up a large version in a div. PHP Code: <p><img id="largeImg" src="images/img1-lg.jpg" alt="Large image" /></p> <p class="thumbs"> <a href="images/img2-lg.jpg" title="Image 2"><img src="images/img2-thumb.jpg" /></a> <a href="images/img3-lg.jpg" title="Image 3"><img src="images/img3-thumb.jpg" /></a> </p> The jQuery Code: Code: $(document).ready(function(){ $(".thumbs a").click(function(){ var largePath = $(this).attr("href"); var largeAlt = $(this).attr("title"); $("#largeImg").attr({ src: largePath, alt: largeAlt }); }); }); With a bit of CSS, this is working fine. What I'm trying to do is to: 1. Add attribute to: PHP Code: <p class="thumbs"> <a href="images/img2-lg.jpg" zoom="images/img2-zoom.jpg" title="Image 2"><img src="images/img2-thumb.jpg" /></a> <a href="images/img3-lg.jpg" zoom="images/img3-zoom.jpg" title="Image 3"><img src="images/img3-thumb.jpg" /></a> </p> 2. Add <a id="img_zoom"> to: PHP Code: <p><a id="img_zoom"><img id="largeImg" src="images/img1-lg.jpg" alt="Large image" /></a></p> 3. Add to the jQuery code, to append the "<a>" link, something like: Code: $(document).ready(function(){ $("img_zoom").append('" href="') $(".thumbs a").click(function(){ var largePath = $(this).attr("href"); var largeAlt = $(this).attr("title"); var zoomPath = $(this).attr("zoom"); $("#largeImg").attr({ src: largePath, alt: largeAlt }); $("img_zoom").html(zoomPath+" " "); return false; }); }); So when the thumbnail image is clicked, the resulting jQuery output would be something like this: Quote: <a id="img_zoom" href="images/img3-zoom.jpg"> Is this do-able? I've played around with it for ages and can't get it to work. Any hlep would be awesome! I want to add a small but informative weather widget to one of my web sites. Having searched around, most are either too big or too simple. I did find "one" that I liked but it links to a lame page. I would rather have no link rather than the one provided. The widget I like is a simple javascript "include file" similar to this one: http://www.gooplusplus.com/external_wx._js Code: // This javascript file is normally dynamically CRON generated // but for this test, we will just use static values. document.write(''+ '<a href="http://www.gooplusplus.com/g5.php?p=X&s=box#FAKE-WEATHER-SITE">'+ '<div class="wxbox"> <img class="wximg" alt="Clear" title="Clear"'+ ' src="http://newcityvegas.com/pics/nightmoon.png" /> '+ ' <div class="wxtemp" title="Temperatu 73°F (23°C)">73°F</div>'+ ' <div class="wxvis" title="Visibility: 10 miles">10 miles</div>'+ ' <div class="wxother"'+ ' title="Wind: Calm, Pressu 1008 hPa (29.76 in Hg), Humidity: 47%">'+ 'Wind: Calm<br />Pressu 29.76 in<br />Humidity: 47%'+ '</div></div>'+ '</a>'); I tried to change the (first and only) HREF URL to a NULL value like this: document.getElementsByTagName('a')[0] = null; It did not work and I have not figured out an alternate method. My working test page is he http://www.newcityvegas.com/weatherbox.html with source code below. Any suggestions? Code: <html><head> <link rel="STYLESHEET" type="text/css" href="weatherbox.css"> </head><body> <script type="text/javascript" src="http://www.gooplusplus.com/external_wx._js"></script> <div style="position:absolute;top:150px;left:0px;margin-left:20px"> <script type="text/javascript"> { // trying to reassign HREF url var firstHREF; var newURL='http://intellicast.com/Local/Weather.aspx?location=USNV0049#subnav'; firstHREF = document.getElementsByTagName('a')[0]; document.write("<font color=red>document.getElementsByTagName\('a'\)\[0\] \=\= </font>"); document.write(firstHREF); document.write("<br><br>"); document.write("<font color=red>Try to change HREF URL value to NULL \=\= </font>"); document.getElementsByTagName('a')[0] = null; firstHREF = document.getElementsByTagName('a')[0]; document.write(firstHREF); document.write("<br><br>"); document.write("<font color=red>Try to set HREF URL value to NEW URL \=\= </font>"); document.getElementsByTagName('a')[0] = newURL; firstHREF = document.getElementsByTagName('a')[0]; document.write(firstHREF); document.write("<br><br>"); } </script> </div></body></html> Hi, I am making a link to the current url and adding ?XXXX to the end of the URL. I have done this like so: Code: <UrlAction Url="javascript:q=(document.location.href);void(open((q)+'?XXXX,'_self','resizable,location,menubar,toolbar,scrollbars,status'))"/> This works perfectly. However I need to add an If statement in this code which does the following: If (there is a ? already in the current url) add &XXXX instead. Can anyone help me add this in? Thanks is it possible to link to a page and then run a javascript all in the same href? for example, i want to redirect back to the home page after a user makes a comment and show the hidden comment div this is my javascript command: Code: <a href="javascript:InsertContent('competitiveevents'); RemoveContent('stories'); InsertContent('blog-comments');"> but can i do something like this..? Code: <a href="../index.php:javascript:InsertContent('competitiveevents'); RemoveContent('stories'); InsertContent('blog-comments');"> Right, so there are a bunch of links in this format: <a href="/profiles/########">username</a> I'm using greasemonkey, and what I want to do is go through and get all of the /profiles/######## part of the anchor tag. I'm using Regex and I can't seem to get a match. This is what I have so far: Code: function ok() { var names = document.getElementsByTagName('a'); var reg = new RegExp("WHAT GOES HERE?"); var e = "links:\n"; var i = 0; while(i<10) { e += reg.exec(names[i].href) + "\n"; i++; } alert(e); } ok(); what is actually desired is I want there to be a way I can select these somehow as well.. every time i try something it doesn't work for me god why is this javascript so hard to make it work... anyway, i just followed the guidelines to pass values from one page to another using ? and it simply, like always, DOES NOT work Code: <HEAD> ... ... <script type="text/javascript"> <!-- function sendme(){ if (document.form1.question[0].checked == true) { whichone = 1; } if (document.form1.question[1].checked == true) { whichone = 2; } if (document.form1.question[2].checked == true) { whichone = 3; } if (document.form1.question[3].checked == true) { whichone = 4; } if (document.form1.question[4].checked == true) { whichone = 5; } if (document.form1.question[5].checked == true) { whichone = 6; } if (document.form1.question[6].checked == true) { whichone = 7; } if (document.form1.question[7].checked == true) { whichone = 8; } myString = qst1 + "=" + whichone; location.href = "Poll_Jan2010_02.html" + '?' + myString; } //--> </script> </head> <body> <div align="center"> <p><img src="63/images/poll_logo.jpg" width="655" height="92"> </p> <p> </p> <h2><u>Question 1</u></h2> <p><strong>Bla bla bla bla...</strong></p> <form name="form1"> <table width="60%" border="0"> <tr> <td><input name="question" type="radio" value="Grill"> option 1</td> </tr> <tr> <td><input name="question" type="radio" value="Club"> option 2</td> </tr> <tr> <td><input name="question" type="radio" value="Crepes"> option 3</td> </tr> <tr> <td><input name="question" type="radio" value="Sandwiches"> option 4</td> </tr> <tr> <td><input name="question" type="radio" value="Hotdog"> option 5</td> </tr> <tr> <td><input name="question" type="radio" value="Special"> option 6</td> </tr> <tr> <td><input name="question" type="radio" value="Refreshments"> option 7</td> </tr> <tr> <td><input name="question" type="radio" value="Salads"> option 8</td> </tr> </table> </form> <p> <a href="#null" class="style2" onclick="sendme();">Continue ></a> </p> </div> </body> Can anyone let me know how can I get the href info dynamically I have 2 different webistes, just want to show those website urls dynamically <html> <body> <a href="http://www.yahoo.com" target="_blank">YAHOO</a> <a href="http://www.Google.com" target="_blank">Google</a> <script type="text/javascript"> document.write(href url of yahoo); document.write('href url of google); </script> </body> </html> thanks for help in advance Can someone see a difference between: <input type="submit" name="Submit" value="Start" onClick="javascript:location.href='Poll_Jan2010_01.html'"> and: <input type="submit" name="Submit" value="Next >" onClick="javascript:location.href='Poll_Jan2010_02.html'"> ??? Because the first works, but the second doesnt :/ All i wanna do is move on to the next page by clicking a submit button. From the first page it moves to second correctly. After that, it only refreshes the page and doesnt move on... |