JavaScript - Please Explain Document.links Property
Hello I am new to javascript and have the following question:
Is it normal that the Document.links property only counts links that are placed before the command? example: Code: <body> <script type="text/javascript"> document.write (document.links.length); </script> <p><a href="http://www.youtube.com/watch?v=gt9j80Jkc_A#">Youtube link</a></p> <script type="text/javascript"> document.write (document.links.length); </script> </body> The result is Quote: 0 Youtube link 1 How can I avoid this? Only by putting the code at the end of the document? And since i am asking? Is it possible to search a entire document for a string and replace that string with one command? (don't give me exact solution please just a hint what direction i should be searching in). Kind regards Similar TutorialsAny help would be awesome and much appreciated! I'm trying to change an id by referencing the previous site the user was on. In this case the user will be coming from either facebook or linkedin. I'm not sure if i'm calling the referrer property correctly. Again, any help is greatly appreciated! <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script language="JavaScript"> if(document.referrer.toLowerCase().indexOf("facebook") != -1) { document.getElementById("phone").innerHTML = "555.555.5555"; } else if (document.referrer.toLowerCase().indexOf("linkedin") != -1) { document.getElementById("phone").innerHTML = "222.222.2222"; } else { //Default code } </script> </head> <body> <p id="phone">123.456.7890</p> </body> I'm new to JavaScript, just started an adult education class in it. I wrote a simple script to view a list of document properties and their values. This part works in Internet Explorer: for (var prop in document){ p_prop = prop; p_list[i] = p_prop + " : " + document[prop] + "<br />"; but doesn't in FireFox. I was wondering how to get the value of a property in FireFox. Thanks. Hello, I have WordPress installed in a directory of my website, and I'm using an iframe on the homepage to embed the blog. I'm using a javascript code that I found online to automatically resize the iframe based on the content inside of it. This is the code: Code: <script type="text/javascript"> function resizeIframe() { document.getElementById('blog').style.height = document.getElementById ('blog').contentWindow.document.body.scrollHeight + 'px'; } </script> This works perfectly in Google Chrome. However, in Firefox I get this error in the console: Code: Error: Permission denied to access property 'document' Source File: http://mysite.com/index1.html Line: 24 I don't have any experience with javascript, but this makes no sense to me, because it should work since the iframe document is on the same domain and server as the parent page. The parent is "mysite.com/index1.html" and the iframe document is "mysite.com/blog/". Why is Firefox complaining about this? It shouldn't violate the "same origin policy" that I have read about. Any help with getting this to work would be appreciated. My code is here and it works ... However, I would like my dynamic table to show on the same page as my body and not on a new blank page. I have created a DIV and try playing around with the document.getElementById('monTab').innerHTML but it's not working out for me ... What am i missing ? Regards, Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD><TITLE>new Script - Javascript Cours 11</TITLE> <META content="text/html"; charset="UTF-8" http-equiv="content-type"> <SCRIPT type="text/javascript"> function createTable(){ var Etudiant = new Array(Number(prompt("How many Students will you put in ?",""))); document.write("<table border=\"1\">"); for (var i=0; i<Etudiant.length; i++) { Etudiant[i] = window.prompt("S'il vous plait entrez le nom d'un etudiant " + (i+1) + ".","") alert("Nice to see you "+Etudiant[i]); document.write("<td>"+Etudiant[i]+"</td>"); j = parseInt(prompt("Combien de notes voulez vous calculez ?")); for (h=0;h<j;h++){ notes[h] = parseInt(prompt("S'il vous plait entrez la "+(h+1)+" note de "+Etudiant[i])); document.write("<td>"+notes[h]+"</td>"); } document.write("<tr>"); } document.write("</tr>"); document.write("</table>"); document.getElementById('monTab').innerHTML=Etudiant; } </script> <BODY> <H1>Combien de note voulez vous cumulez ?</H1> <br> <br> <input type="button" name="btnSubmit" value="TRY IT" onclick="createTable()"> <div id="monTab" size="10"> Content should come here ...Content should come here ...Content should come here ...Content should come here ...Content should come here ...Content should come here ...Content should come here ... </div> </BODY> </HTML> <script type="text/javascript"> function display(action, id) { if (action == 'show') { document.getElementById("explanation"+id).style.display = "block"; document.getElementById("link"+id) = "javascript:display('hide', "+id+")"; } if (action == 'hide') { document.getElementById("explanation"+id).style.display = "none"; document.getElementById("link"+id) = "javascript:display('show', "+id+")"; } } </script> i got this code from this thread http://www.codingforums.com/showthread.php?t=241809(THanks to jmrker) and i was wondering if you could explain how it works(The creating textboxes and the randomization). Edit: SOrry i forgot to include the code Code: <!DOC HTML> <html> <head> <title> Untitled </title> <script type="text/javascript"> // From: http://www.codingforums.com/showthread.php?t=241809 var maxSeats = 341; var rndSeats = 10; function randOrd() { return (Math.round(Math.random())-0.5); } function createDisplay() { var seating = [];; var str = ''; for (var i=0; i<maxSeats; ++i) { seating.push(0); } for (var i=0; i<maxSeats; i++) { str += '<input type="text" size="1" id="s'+i+'" value="'+seating[i]+'">'; if ((i % 33) == 32) { str += '<br>'; } } document.getElementById('auditorium').innerHTML = str; } var sarr = []; function randomize() { for (var i=0; i<maxSeats; i++) { sarr[i] = i; } sarr.sort(randOrd); } var tptr; var ptr = 0; function pauseDisplay() { document.getElementById('s'+sarr[ptr]).value = 'x'; document.getElementById('s'+sarr[ptr]).style.backgroundColor = 'red'; if (ptr < (maxSeats-rndSeats)) { ptr++; tptr = setTimeout("pauseDisplay()",250); } } window.onload = function() { createDisplay(); randomize(); tptr = setTimeout("pauseDisplay()",250); } </script> </head> <body> <div id="auditorium"></div> </body> </html> hi, i would like to know what kind of mechanism does the bolded part trigger Code: G = function(a) { if (i !== a) { var b = i; i = a; for (a = 0; a < A.length; ++a) A[a](b) } } i dont want to know what the function does, just a wild guess, would it be the A array is replaced with functions names? R. I tried a a few tutorials but still can't get it, can any simply closures please?
<script type="text/javascript"> var myString = "zero one two three four"; var mySplitResult = myString.split(" "); for(i = 0; i < mySplitResult.length; i++){ document.write("<br /> Element " + i + " = " + mySplitResult[i]); } </script> "split" function splits the string whenever it encounters "space". But please explain me how the strings are stored in "mySplitResult". And can we use Arrays for this program? If so, can you code it. Thanks in advance! So today I learned about the ()'s for functions so I was like always studying and I cannot understand how this snippit works, here it is. Quote: var say = function(what) { what('say function'); } var hello = function (who) { alert('Hello '+who); // outputs "Hello say function" } say(hello); So the say(hello); is activating the function. The (hello) should be passed for (what) so the function(who) is now inside the other or something? Can someone please list steps like 1-10 of how this code ends up saying "Hello say function" at the end? thank you so much umm Hi i wrote a script with little help from internet but i dont truly understand it, can anyone please explain to me what every line in this script is doing ? im asking polite. var przed="zostalo do urodzin" var obecnie="dzisiaj urodziny" var Tablica=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") function odliczanie(r,m,d){ theyear=r;themonth=m;theday=d var dzisiaj=new Date() var rok=dzisiaj.getYear() if (rok < 1000) rok+=1900 var miesiac=dzisiaj.getMonth() var dzien=dzisiaj.getDate() var godzina=dzisiaj.getHours() var minuta=dzisiaj.getMinutes() var sekunda=dzisiaj.getSeconds() var dzisiajstring=Tablica[miesiac]+" "+dzien+", "+rok+" "+godzina+":"+minuta+":"+sekunda dataa=Tablica[m-1]+" "+d+", "+r dd=Date.parse(dataa)-Date.parse(dzisiajstring) dday=Math.floor(dd/(60*60*1000*24)*1) dgodz=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1) dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1) dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1) if(dday==0&&dgodz==0&&dmin==0&&dsec==1){ document.forms.count.count2.value=obecnie return } else document.forms.count.count2.value=dday+ " dni, "+dgodz+" godzin, "+dmin+" minut, i "+dsec+" sekund "+przed setTimeout("odliczanie(theyear,themonth,theday)",1000) } odliczanie(2015,1,20) I've been playing with this workarouns for literally years, but I'm getting fed up with it. Can anyone here suggest a better way of dealing with the following conundrum - or at least clarify why it occurs? When I have an external stylesheet I can't access the properties assigned therein. I'll give an example - HTML/CSS/JS are all inseperate files - but everything is called into the HTML as you can see. HTML: Code: <head> <script type="text/javascript" src="js.js"></script> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="box" onclick="showWidth()"></div> <body> CSS (style.css) Code: #box { width: 200px; } Javascript (js.js) Code: function showWidth(){ var box = document.getElementById('box'); alert(box.style.width); } Now the alert fired by showWidth() is empty - meaning that javascript can't see the width of the div element 'box' Now, if I assign width as part of the style attribute of the div javascript thus: HTML: Code: <head> <script type="text/javascript" src="js.js"></script> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="box" onclick="showWidth()" style="width: 200px;"></div> <body> javascript can see this and the alert fired by showWidth() shows '200px'. So, I have two questions: 1. Why? 2. Is there a way of accessing these external style elements without having to define styles inline all the time? Cheers! Mike Hi all, First of all, I am not a student and this is not homework. Secondly, I've been programming in C, Motorola assembler, Intel assembler and even GWBasic for years. I recently (this year) got into Web / Javascript / PHP / HTML programming and I'm clawing up the learning curve. I know a fair amount, but have a long way to go. OK here's my problem: I've been trying to integrate a WYSIWYG editor (TinyMCE) into a bulletin board software package (PHPBB3). All is working well except for one big stumbling block that I've been battling for the past MONTH!...: I want to support the original BBCode system of PHPBB3 (mostly because of the ability for the admin to add custom BBCodes). So, what I need to do is this: (1) Select a range of text. (2) Either REPLACE it with "[tag]selection[/tag]" or else INSERT "[tag]" before and "[/tag]" after. (3) Lastly, the original selection must remain selected so that additional BBCodes can be wrapped without the need to re-select. The purpose of (3) is, say, the user clicks "bold" and "italic" and "underline".... all they should have to do is click those 3, not re-select each time. I've tried doing this: (1) get the selection range (2) get the selection text (3) delete the range contents (4) create two "contextual fragments" (one for the opening tag, the other for the closing tag). (5) create a <span> element containing the selection text (6) Insert it all into the range with range.insertNode() (7) finally select the new span element This seems to work fine, but Internet Explorer fails (it complains when I try to get the selection range). I just don't know what I'm doing wrong... and worse I'm not even sure if I'm attacking the problem the proper way. Any ideas would be greatly appreciated! Thanks! -- Roger I found this line in one of the scripts on javascript.kit <code> if(document.layers|| document.getElementById|| document.all) </code> Could someone give me an idea on why this line would be used in a code? Thank you very much OK, I work for a company that accepts donations. Currently, the donation page looks nothing like the rest of the site. So I recreated the page to look like the rest of the site. But when I do so, Chrome shows it as a "security risk". Someone looked at it and told me that it's because the .js files aren't compressed and the browser considers this a security risk. So how do I compress the files? overImg=[];outImg=[]; outImg[0]=new Image();outImg[0].src="images/enter1.gif"; overImg[0]=new Image();overImg[0].src="images/enter2.gif"; outImg[1]=new Image();outImg[1].src="images/keio.gif"; overImg[1]=new Image();overImg[1].src="images/inria.gif"; function chgImg(img,type){ switch(type){ case "over":document.images[img].src=overImg[img].src;break; case "out":document.images[img].src=outImg[img].src;break; }} why are arrays used here ? Hi, I am hoping a JS expert might be able to explain this function. It is part of a larger programme. The program basically checks which profile the user selects and sets the html to display in certain colours. I am still trying to understand programming. The part I don't understand is the SetCookie("profile", i); What is this function telling the SetCookie function to do? Why does this work? Really grateful for any info.Thanks in advance. ************************************************* function updateProfile() { for (var i=0;i<document.form1.profile.length;i++) { if (document.form1.profile[i].checked) { SetCookie("profile", i); } } document.location="cookie3.html"; } Please, if you would explain this regex fragment [^>]* to me. Thank you for reading this post. (I know how to use the ThankYou button ) PHP Code: <script type="text/javascript"><!--//--><![CDATA[//><!-- sfHover = function() { var sfEls = document.getElementById("nav").getElementByClass("subMenuWrapper"); for (var i=0; i<sfEls.length; i++) { sfEls[i].onmouseover=function() { this.className+=" sfhover"; } sfEls[i].onmouseout=function() { this.className=this.className.replace(new RegExp(" sfhover\\b"), ""); } } } if (window.attachEvent) window.attachEvent("onload", sfHover); //--><!]]></script> Im trying to get the sub navigation to work in ie 6 but can get it working.. can anybody tell me what the above funtion does and how i might be able to apply it to my code... the navigation i have so far is on http://www.corkdesigns.ie/ep3/testing.html Hope someone can help or is the a simple hack i can use for ie 6 for this navigation http://www.corkdesigns.ie/ep3/navigation1.html Thanks So I had the orginial code just switch images, I was under the impression I could use "var clientData" to make an array. However this is not working out the way I had hoped. Any help or explanation, greatly appreciated. It's very sloppy now! Don't judge. Here is my Javascript: Code: <script type="text/javascript"> var imgList = [ "../Assets/Images_Revised/40_kitchen.jpg", "../Assets/Images_Revised/40_stair.jpg", "../Assets/Images_Revised/C_front2.jpg", "../Assets/Images_Revised/C_rear_side_combo.jpg", "../Assets/Images_Revised/C_side.jpg", "../Assets/Images_Revised/Y_combo.jpg", "../Assets/Images_Revised/Y_window.jpg" ]; var clientData = [ "This is Data for Client 1"; "This is Data for Client 2"; "This is Data for Client 3"; "This is Data for Client 4"; "This is Data for Client 5"; "This is Data for Client 6"; "This is Data for Client 7" ]; var currentMain = 0; var currentMainT = 0; function Prev() { return ShowMain(currentMain-1); return ShowMainT(currentMainT-1); } function Next() { return ShowMain(currentMain+1); return ShowMainT(currentMainT+1); } function ShowMain(which) { currentMain = which; currentMainT = which; if ( currentMain < 0 ) currentMain = 0; if ( currentMainT < 0 ) currentMainT = 0; if ( currentMain > imgList.length-1) currentMain = imgList.length-1; if ( currentMainT > clientData.length-1) currentMainT = clientData.length-1; document.getElementById('mainImg').src = imgList[currentMain]; document.getElementById('mainText').src = clientData[currentMainT]; var PD = document.getElementById('Pg'); var PD2 = document.getElementById('Pg2'); if (PD != null ) PD.innerHTML = 'Image '+(currentMain+1)+' of '+imgList.length; if (PD2 != null ) PD2.innerHTML = (currentMainT+1)+' of '+clientData.length; return false; } onload = function() { ShowMain(0); } onload = function() { ShowMainT(0); } </script> Here is my area I am showing it. Code: <a href="#" onclick="return ShowMain(0); return ShowMainT(0)">1</a ><a href="#" onclick="return ShowMain(1); return ShowMainT(1);">2</a ><a href="#" onclick="return ShowMain(2); return ShowMainT(2)">3</a ><a href="#" onclick="return ShowMain(3); return ShowMainT(3)">4</a ><a href="#" onclick="return ShowMain(4); return ShowMainT(4)">5</a ><a href="#" onclick="return ShowMain(5); return ShowMainT(5)">6</a ><a href="#" onclick="return ShowMain(6); return ShowMainT(6)">7</a ><a href="#" onclick="return Prev();" class="gallery"><</a ><a href="#" class="gallery" onclick="return Next();">></a> <span id="mainText">Click on a Client for Information.</span> |