JavaScript - Inserting Img Links
So my goal was to insert a mini ICO image (of an R) and have it be a link to an external news page for each individual player on a fantasy team. The code below gets the proper link to each player and inserts the image and corresponding link one at a time though a couple different functions. I have inserted the IMG successfully (see attached), but I am having issues with the link. There are times when the images load accurately with the correct link and others when some of the 'href' elements say undefined. And it isn't consistent either, as to imply that it is a specific coding error. It is completely random and sometimes they are even all correct. Could this have something to do with the asynchronicity of the GM_xmlhttprequests and the loop? If so, is there some way to get around that?
This is the bottom of the code which gets fed an array teamURL with all the proper URLs. All of the variables and arrays have the correct information, as they have been checked and double checked. It is almost as if I need a way to slow down the loop to let it do its thing... if that makes any sense. Code: var count = 0; var item = 0; for (var k=0; k< myPlayersArray.length; k++) { GM_xmlhttpRequest({ method:"GET", url:teamURL[k], headers:{"User-Agent":"Mozilla/5.0","Accept":"text/xml"}, onload:function(response){findInfo(response);} }) ; } var allElements = document.evaluate("//*[contains(@href, 'http://sports.yahoo.com/nfl/players/')]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null); var baseRotoworldURL = 'http://www.rotoworld.com'; function findInfo( response ) { var doc = document.createElement( "div" ) ; doc.innerHTML = response.responseText ; //Get appropriate td data on the rankings page var teamPageList = document.evaluate("//a[contains(@href, 'player')]",doc,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null); for (var m=0; m<teamPageList.snapshotLength; m++) { if ((teamPageList.snapshotItem(m).innerHTML.toString()) == (myPlayersArray[count].toString())) { var playerHTML = teamPageList.snapshotItem(m).parentNode.innerHTML; var match = playerHTML.match(/\/nfl\/\d{1,10}\/[a-z]+\-[a-z]+/); var playerURL = baseRotoworldURL +"/recent" + match break; //loop until you get a hit } } addGameLogIcon(allElements, playerURL); count++; } function addGameLogIcon(allElements, playerURL) { var thisElement; var rotoworldLink; var rotoworldLinkGraphic = "data:image/gif;base64,"+"AAABAAEADw8AAAEAGAA0AwAAFgAAACgAAAAPAAAAHgAAAAEAGAAAAAAAAAAAAEgAAABIAAAAAAAAAAAAAADR09TR09TR0tPR0tPQ0tPQ0tPQ0tPQ0dTQ0tPQ0tPQ0tPQ0dPQ0tPR09TR09S9AADR09TR09TP0dOvssuan8acocfFx9HQ0tPGyNGdocean8edocfDxdDR09TR09S9AADR09TR09TGydFHRsEsKNsrJ9SGicLP0dNsb74rJ9csKNsrJ8iTlcPR09TR09S9AADR09TR09TEx9BFRcYvKu0sKOOChcK3ucwuKsgvKu0uKedUVMDIydHR09TR09S9AADR09TR09TEx9BERMQuKOcsJ9+ChcFnaLstJ+IuKOc0McuqrMnQ0tTR09TR09S9AADR09TR09TEx9BERMItKOEsJ9lfYbYuKsQtKOEsJ9hxc7/P0NPQ0tPR09TR09S9AADR09TR09TEx9BERMAsJ9srJ9gqJsgrJ9ksJ9sqJsdub7vJytHQ0tPR09TR09S9AADR09TR09TEx9BEQ70sJ9QsJ9UsJtQsJ9QsJtUsJtQrJspzdb3Q0dPR09TR09S9AADR09TR09TEx9BEQ7stJc4sJco8OrZJSLkxLbotJc0tJs49Ori/wM7R09TR09S9AADR09TR09TEx9BDQ7ksJ8gsJsN6fL3Bw855e7wrJcIsJ8c5Nrm7vs3R09TR09S9AADR09TR09TEx9BEQ7YtJ8IsJ780MLE+O7QuKrYsJ8EtJ8I/PLTBws/R09TR09S9AADR09TR09TEx9BERLUtKMEtKMEtJ8EtJ8EtJ8AsJ8EtKLh7fL3P0NTR09TR09S9AADR09TR09THytFMTrQpJrUpJrUpJrUpJrUvLrVGRLSKisDLzNLQ0tTR09TR09S9AADR09TR09TR09TDxdC7vc67vc67vc67vc6+wc/HytHR09TR09TR09TR09TR09S9AADR09TR09TR09TR09TR09TR09TR09TR09TR09TR09TR09TR09TR09TR09TR09S9AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="; thisElement = allElements.snapshotItem(item); rotoworldLink = document.createElement('a'); rotoworldLink.setAttribute("href", playerURL); rotoworldLink.setAttribute("target","_blank"); rotoworldLink.innerHTML = "<img border='0' src='" + rotoworldLinkGraphic +"'>" ; thisElement.parentNode.appendChild(rotoworldLink, thisElement.nextSibling); item=item+3; } Similar TutorialsFirst off: I know nothing of Javascript (or much of any coding really), but I expect this to be simple for someone experienced, although it was (suprisingly) hard to figure out, for a lousy Googler like myself; so here I am. I have a function that generates an URL to an image, as so: Code: function todaysDate() { var currentTime = new Date() var month = currentTime.getMonth() + 1 var day = currentTime.getDate() window.location.href = "http://www.ecample.com/generator.aspx?format=png&month="+month+ "&day="+day+"&width=320&height=480" } I get the image displayed fine in a new window, but I need it to open in a <div> or object, iframe, something I can place under my <div>-based header, when a button is pressed from my menu. Code: <div id="image"> <div class="header"> <a href="#" class="back">back</a> <h1>Title</h1> </div> I WANT IT HERE! </div> Any help would be greatly appreciated! Regards, Andrew. I have a table with two rows and 4 columns, the first cell has rowspan of 2. How do I insert something into the first cell only? How can I get the cell index of the first cell?
How would I insert bbc code around selected text in a textbox like it is in this forum? I'm quite new to javascript, so please try to explain it simply Thank you, ~AngelMare Hello. I have two javascript problems. 1). I have a java script that displays 'What's On Air Now' and 'Today's Schedule' which are located in a 'Resources' folder on our website. I have been using Namo Web Editor for several years but, after many problems, I bought WebPlus X4 from Serif Applications. It's a very good program but I'm having severe problems inserting the Javascripts mentioned above. With Namo, I just had to add: "<script type="text/javascript" src="resources/onair.js"> and the same for 'Today's Programs':"<script type="text/javascript" src="resources/schedule.js">". with WebPlus X4, I've had no luck after trying numerous times and requesting help from Serif who would not help. I would be grateful if anyone could help me, please. Thank you. PS: I have attached the text version of the scripts for you to have a look at.
Hello, I know very little Javascript, so the answer to this is probably VERY simple :-) I have some javascript working out the current date for me. I want to output this date into the value of an input field in a form. My javascript to output the date is: Code: <script type="text/javascript"> <!-- var currentTime = new Date() var month = currentTime.getMonth() + 1 var day = currentTime.getDate() var year = currentTime.getFullYear() document.write(month + "/" + day + "/" + year) //--> </script> And I'd like this to output in the value of the input field below: Code: <input type="hidden" name="date" value="js date to go here"/> I've tried using script tags and document.write, but it messes up my HTML and I'm not sure what exactly it is that I'm doing wrong!! Sorry for the silly question... Thanks for your help Nicky Hi, I am sending some information from one page to another using Form and Link. This generates a string of text in the receiving url, which I am extracting and displaying in a textarea. How do I insert a linebreak in the javascript code? With this line of code: document.getElementById("Requirements").value = "Top line: "+ topline + "Your breakfast contains: "+ menuitems; everything that is being returned is running together. I need it to run on separate lines. I can't control it with box width because the entries will vary on each occasion. I assume there is an escape character like in php or a break tag like in HTML, but I don't know what it is! Any help appreciated. Thanks, Tim Hi all, I have: Code: var moreinfo =""; moreinfo+= "here some content"; $("#divMoreInfo").html(moreinfo); And this inserts "here some content" in a div called divMoreInfo. Now divMoreInfo is placed inside an iframe while the rest of the programming has to be in the parent page... How do I insert moreinfo's contents in the iframe's div?? Thanks a lot Hi im new to this site and im a begginer, so forgive me for any grammatical errors please. How would I go about running a program onclick or onblur and displaying the result on same page without it changing? What i'm trying to do is, insert a file upload input into HTML, depending on dropdown menu selection. I have a form, where you enter your email and a message. What i'm trying to add now is, a dropdown menu which will select type of message, with 4 options. If 3rd or 4th option is selected, a <input type="file"> will be inserted under the message box. I also don't want the page to refresh. here's the code i have now: Code: <form action="zahteva.php" name="naroci" method="POST"> <label>Your email:</label> <input id="text" type="text" name="email" autocomplete="off"> <br /> <label>Type</label> <select name="type"> <option value="1">1st (no file upload)</option> <option value="2">2nd (no file upload)</option> <option value="3">3rd (file upload)</option> <option value="4">4th (file upload)</option> </select> <label>Message:</label> <textarea id="txtrea" name="sporocilo"></textarea> <br /> <!-- <label>File:</label> <input type="file" name="file"> only show this part if 3rd of 4th is selected --> <input id="gump" type="submit" value="Send"></td> </form> @ http://jsfiddle.net/defencedog/cu5VJ/ I am having problem in understanding the operation of js with regards to this line Code: $('.' + x[i].id).text(x[i].innerHTML); or Code: $('.' + x[i].id).html(x[i].innerHTML); I want to understand why I ain't able to produce desired results while using this instead Code: $('.' + x[i].id).innerHTML(x[i].innerHTML); Hi, I am using javascript to collect hidden form elements and then send the values to a php page, without reloading or changing the page. Basically i have a list of job vacancies, with just an apply button, when a person clicks on the apply button, the form collects some information from their account and sends it using the javascript code. Now this works fine, except for if somebody then clicks on the second job it sends the information from the first form again. And it seems to remember the information. What i am trying to find out is it possible to have multiple forms on one page which once submitted send variables to javascript which then send them to a php but clear themselves so that it doesn't remember the previous values. Here is the code for the form: Code: <form id="submit" method="post"> <fieldset> <input type="hidden" name="usern" id="usern" value="php variable" /> <input type="hidden" name="joborder_id" id ="joborder_id" value="php variable" /> <input type="hidden" name="site_id" id ="site_id" value="php variable" /> <button class="button positive">Apply For This Job </button> </fieldset> </form> <div class="success" style="display: none;">Your Application has been sent.</div> </form> And the Javascript code is he Code: $(document).ready(function(){ $("form#submit").submit(function() { // we want to store the values from the form input box, then send via ajax below var usern = $('#usern').attr('value'); var joborder_id = $('#joborder_id').attr('value'); var site_id = $('#site_id').attr('value'); $.ajax({ type: "POST", url: "apply.php", data: "usern="+ usern +"& joborder_id="+ joborder_id +"& site_id="+ site_id, success: function(){ $('form#submit').hide(function(){$('div.success').fadeIn();}); } }); return false; }); }); Any ideas or suggestions would be appreciated, Thanks Lee I am trying to insert a new paragraph with javascript, however my code is not working, even though i pretty much copied my code from the book. Could someone please tell me why i have a problem with a script below? Thank you very much. <script type="text/javascript"> function newtext() { var myPara=document.createElement("p"); var text="We sincerely hope that you are enjoying learning Javascript"; var paratext=document.createTextNode(text); myPara.appendChild(paratext); } </script> <p> <a href="#" onclick="newtext()"; return: true;> Insert new text </a> </p> I'm trying to insert a form into a div, based on what the user selects in another select form. I'm getting an error whenever I try typing my code. I'm doing this in Dreamweaver and it highlights my text green (starting at the first /td and ending at the next / of the next /td) Will adding this form even function properly when I send? Any ideas on what's going on? Thanks Code: function showTable(y) { if(y == "readers") { document.getElementById("next").innerHTML = "<form id='readersTable'> <table> <tr> <td>Attribute 1</td> <td><input type='text' name='at1'> </td> </tr> </table> </form>" } Hello, I need your help, I would to like to insert some styled text into an HTML document using innerHTML and a Div. What would be the code to insert some text with a different font, size and color? Any help is appreciated. Thanks, J Hey. Yes, I'm just starting at the beginning and what I'm trying to understand today is adding special characters such as line break, quotes, etc. into Javascript. I'm missing something, however. Can someone please assist? What I'm trying to do is add a line break without having it show /n on the screen. My Code: <html> <head> <title>Exercise 6-20</title> </head> <body><pre> ********* *** * *\n * * * * *** * *\n * * * * ***** * *\n * * * * * * *\n * * * * * * *\n * * * * * * *\n * * * * * * *\n * * * * * * *\n * * * * * * *\n * * * * * * *\n * * * * * * *\n ********* *** * * *\n </pre> </body> </html> Thank you, H I want to insert text to a textarea based on the selected text. It should insert tags like the buttons for posting on this form work. Ie: If 'bar' is selected, and I click 'b', then I get: Code: Foo bar frog becomes: Code: Foo <b>bar</b> frog I found some code that works for firefox, but I need it to work in explorer as well. Do you have any ideas or examples how to do this? Here's the code I'm currently using. ( Works right in ff3. In IE7 it appends the tags at the end of the text. I want it to insert based on my selection. ) Usage: Code: <a onclick="insertTags('<b>', '</b>', document.forms.formpost.text);return false;" href="javascript:void(0);">[ Bold ]</a> insert function: Code: <script type="text/javascript"> // Surrounds the selected text with text1 and text2. function insertTags(text1, text2, textarea) { // Can a text range be created? if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) { var caretPos = textarea.caretPos, temp_length = caretPos.text.length; caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2; if (temp_length == 0) { caretPos.moveStart("character", -text2.length); caretPos.moveEnd("character", -text2.length); caretPos.select(); } else textarea.focus(caretPos); } // Mozilla text range wrap. else if (typeof(textarea.selectionStart) != "undefined") { var begin = textarea.value.substr(0, textarea.selectionStart); var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart); var end = textarea.value.substr(textarea.selectionEnd); var newCursorPos = textarea.selectionStart; var scrollPos = textarea.scrollTop; textarea.value = begin + text1 + selection + text2 + end; if (textarea.setSelectionRange) { if (selection.length == 0) textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length); else textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length); textarea.focus(); } textarea.scrollTop = scrollPos; } // Just put them on the end, then. else { textarea.value += text1 + text2; textarea.focus(textarea.value.length - 1); } } </script> Code: var ip = '<!--#echo var="REMOTE_ADDR"-->'; xmlhttp.open("GET","getuser.php?q="+str+"&ip="+ip,true); xmlhttp.send(); I'm sending the above via a GET to a php file that inserts into a DB. here is the php portion Code: <?php $q=$_GET["q"]; $ip=$_GET["ip"]; $sql="INSERT INTO statcounter (selection, ip) VALUES ('".$q."','".$ip."')"; It is inserting properly into the DB, but the IP isn't correct. In the IP field I'm getting "<!--" instead of the users IP address. Would anyone have any advice on this? I took the first line from this website http://javascriptsource.com/user-det...p-address.html Apparently they were able to get the IP from only using var ip = '<!--#echo var="REMOTE_ADDR"-->'; But I'm not sure how they were able to do this So I've got an order form that uses javascript to immediately calculate the price as soon as you enter the quantity that you want to order: Code: <form method="post" action="submitted.html" name="form"> <table id="order-table"> <tr> <td>Widgets</td> <td><input type="text" name="quantityWidgets" onchange="calculate()"></input></td> <td> x </td> <td>$650</td> <td>=</td> <td><input type="text" name="totalWidgets" disabled="disabled"></input></td> </tr> </table> </form> And here's the Javascript Code: function calculate(){ total = document.form.quantityWidgets.value * 650; document.form.totalWidgets.value = total; } But that's just for 1 row and I have several rows of products. I could just use a different calculate() function for each row, but I'm trying to pass arguments into calculate() AND make it display the result in the proper place, but my syntax must be wrong. Code: ... <td><input type="text" name="quantityWidgets" onchange="calculate(quantityWidgets, 650, totalWidgets)"></input></td> ... function calculate(quantity, price, total){ total = document.form.quantity.value * price; document.form.total.value = total; } Can anyone help me please? |