JavaScript - If Statement Using Source Of An Image
I am currently trying to write a javascript function which checks whether the source of an image is one thing, if so do this, if not do something else.
Any help is much appreciated. Below is the code I have tried... Code: if(document.getElementById("field").src == "images/tick.png"){ alert("this is the tick image"); } Similar TutorialsI cannot get this function to work properly. Any help would be appreciated, please understand i am not so good at scripting i am trying to check the source of multiple images in one if statement to run a sound. Code: function victory() { if (document.getElementById("Image0").src == "0.jpg" + document.getElementById("Image1").src == "1.jpg" + document.getElementById("Image2").src == "2.jpg" + document.getElementById("Image3").src == "3.jpg" + document.getElementById("Image4").src == "4.jpg" + document.getElementById("Image5").src == "5.jpg" + document.getElementById("Image6").src == "6.jpg" + document.getElementById("Image7").src == "7.jpg" + document.getElementById("Image8").src == "8.jpg" + document.getElementById("Image9").src == "9.jpg") { document.getElementById("vic").currentTime = 0; document.getElementById("vic").play(); } else{ } } Reply With Quote 01-21-2015, 05:19 PM #2 Dormilich View Profile View Forum Posts Senior Coder Join Date Jan 2010 Location Behind the Wall Posts 3,532 Thanks 13 Thanked 372 Times in 368 Posts + is the addition, concatenation and number casting operator. the logical AND operator is &&. cf. https://developer.mozilla.org/en-US/..._and_Operators Very new to Javascript (been a HTML/CSS guy for a while, but never really got into more than tinkering with other other people's Javascript coding), but I came across a situation I can't find canned coding for, and trying to create it from bits and pieces I Googled has led me to a dead-end. In summary, I have a website that I frequently visit (FWIW, Firefox 3.x is my browser of choice) with many image sources referring to URLs that end with "-thumbnail.jpg". However, for better image quality, I am trying to use Greasemonkey to replace all instances of "-thumbnail.jpg" in the source of images on this site with "-bigthumbnail.jpg". The closest I could think of was to somehow use getElementsByTagName and innerHTML.replace, but realized that innerHTML does not do HTML, only content. Below is as far as I tried to get on my own, I appreciate any assistance from the folks here to help a JS novice like myself, thanks! Code: (function() { var as,ae; as = document.getElementsByTagName("img"); for (var i = 0; i < as.length; i++) { ae = as[i]; ae.innerHTML = ae.innerHTML.replace(/-thumbnail/gi, "-bigthumbnail"); } })(); Greetings, First off - You can see my problem at psychopathgames.com I have an image stretched to fit as the page background in a DIV and the page content inside a seperate DIV. When you mouseover the links, the background changes to represent each link (as does the color of the link... but that is working fine). Trouble is, the images that are being pulled aren't fullsized as the main background is... it is being cut off by some code I can't figure out and isn't resizing. Please visit the site to see what talking about. Here is the code... Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Psychopath Games</title> <style type="text/css"> html, body {height:100%; margin:0; padding:0;} #page-background {position:fixed; top:0; left:0; width:100%; height:100%;} #content {position: relative; z-index:1; padding:10px;} </style> <!--[if IE 6]> <style type="text/css"> html {overflow-y:hidden;} body {overflow-y:auto;} #page-background {position:absolute; z-index:-1;} #content {position:static;padding:10px;} </style> <![endif]--> <style type="text/css"> A:link {text-decoration: none} A:visited {text-decoration: none} A:active {text-decoration: none} A:hover {text-decoration: none} </style> <script language="JavaScript"> <!-- img0_on = new Image(100,11); img0_on.src="images/titlesred.png"; img0_off = new Image(100,11); img0_off.src="images/titles.png"; img1_on = new Image(130,11); img1_on.src="images/contactred.png"; img1_off = new Image(130,11); img1_off.src="images/contact.png"; img2_on = new Image(170,11); img2_on.src="images/talentred.png"; img2_off = new Image(170,11); img2_off.src="images/talent.png"; function over_image(parm_name) { document[parm_name].src = eval(parm_name + "_on.src"); } function off_image(parm_name) { document[parm_name].src = eval(parm_name + "_off.src"); } --> </script> <script type="text/javascript"> function showIt1(imgsrc) { newImage = "url(images/backgroundone.jpg)"; document.getElementById('content').style.backgroundImage = newImage; } function showIt2(imgsrc) { newImage = "url(images/backgroundtwo.jpg)"; document.getElementById('content').style.backgroundImage = newImage; } function showIt3(imgsrc) { newImage = "url(images/backgroundthree.jpg)"; document.getElementById('content').style.backgroundImage = newImage; } function hideIt() { newImage = "url(images/background.jpg)"; document.getElementById('content').style.backgroundImage = newImage; } </script> </head> <body> <div id="page-background"> <img src="images/background.jpg" width="100%" height="100%"> </div> <div id="content"> <center> <img src="images/title.png"> <br><br> <br> <table width="100%"> <tr> <td width="40%" align="right" valign="bottom"> <a href="titles.html" onmouseover="over_image('img0'); showIt1(this.src);" onmouseout="off_image('img0'); hideIt();"> <img src="images/titles.png" border="0" name="img0"> </a> <br> <br> <a href="contact.html" onmouseover="over_image('img1'); showIt2(this.src);" onmouseout="off_image('img1'); hideIt();"> <img src="images/contact.png" border="0" name="img1"> </a> <br> <br> <a href="talent.html" onmouseover="over_image('img2'); showIt3(this.src);" onmouseout="off_image('img2'); hideIt();"> <img src="images/talent.png" border="0" name="img2"> </a> <br> <br> </td> <td width="60%"> <center> <img src="images/eye.png"> </td> <tr> </table> </div> </body> </html> This is really annyoing. I'm trying to set up an image's source from a function within the document's head, but to no avail. The code is something like this: ------------------------------------------------------- <head> .....js code..... function updatePieChart() { document.pie_chart.src = "piechart.php?p1&p2"; // php-generated image, with parameters } // A FUNCTION WHICH SUCCESSFULLY RUNS EVERY 2-3 SECONDS function ajax() { ....stuff..... // THE NEXT LINE DOESN'T WORK!!! // updatePieChart(); ...stuff..... } ....js code...... // THE NEXT LINE DOESN'T WORK!!! // updatePieChart(); </head> //THIS LINE WORKS LIKE A CHARM <body onload="updatePieChart();"> ....bla bla bla... <img src="#" name="pie_chart" /> ...bla bla bla.... </body> ------------------------------------------------------- As you can see, the function updatePieChart works wonderfully from the <body> tag of the function, but any attempt to call it from within the <head> tags fails. Also, I've tried to change the function updatePieChart so that it wouldn't call any php code and changed it to a simple document.pie_chart.src = "#"; but that still causes errors on the page when called from inside the <head> tags. On the other hand, if the function updatePieChart merely made an alert() call, then the ajax element works fine, and every so and so seconds I get an alert. Anyone, help? An explanation how come I can't set the image from within the <head> tags? I need to change the source of an image but I can't seem to figure out how. I believe that I have the correct syntax, but it does not seem to be working. Maybe I am typing it in wrong or something. I try to test my javascript by typing the script: <strong>javascript:document.getElementById('img1').src='./images/image1.jpg';</strong> into the address bar. Nothing happens. I also try to test it out by putting this on the page, but once again, nothing happens. Code: <script type="text/javascript"><!-- document.getElementById('im1').src="./scripts/'.$upload_image.'"; //--></script> This is what my HTML code looks like: Code: <img src="./images/temp_wizard_picture.gif" alt="" style="padding:4px; background:#fff; border:1px #bbb solid;" id="img1" /> Can somebody please fill me in on what I am doing wrong. Thank you. recently i noticed that all of my embedded JavaScript code and external style sheets are being shown IN "view source." this happens in all browsers ( IE, FF, Chrome, and Safari [windows]) ... oddly enough it only happens when viewing on my vista or win2k3 machines. is this something added to these OS's or the result of an installed program? has anyone seen this before? I am new to this and I am pretty lost, basically I want the div to start out completely minimized at 0% height and then when I click the button that calls the function it should increase the size to 100%, once it is maximized I want it to set the height back to 0% when the button is clicked again. Heres what I have so far. I can get it to minimize and maximize itself but I can't get the height set correctly. Code: <SCRIPT language="javascript"> var y = 100; var q = 5; var f = 0; var g = 0; function changeheight(){ if(y>170&&g==0){g=1;return;} if(y<101&&g==1){g=0;return;} if(g)q=-5;if(!g)q=5;y=y+q; e=document.getElementById("examplediv"); e.style.height = y + 'px'; t=setTimeout("changeheight();",0); </SCRIPT> Can anyone help me out, or is there an easier way to do this? Basically I want to have a different background image for every time of the day. The function by itself works okay with onclick or onload placed in body. Code: function changesky() { document.getElementById("sky").style.background="url(back_morning.jpg) repeat-x"; } What I want to do is have my background change in addition to a message. So instead of just "Good Morning" text I also want the background to change. Code: if (time>=4 && time<12) { document.write ("<p>Good Morning!</p>"); document.getElementById("sky").style.background="url(back_morning.jpg) repeat-x"; } Tried this, but it didn't work so what's the format for calling a function inside of an if statement? Or what other method should I use to do this? Hello everyone, I am using javascript and I have a radio button that the user selects and I have a function to see which button was selected, but i'm trying to use the return of that if statement in another statement. Basically another part of the function is dependent on what the user selects in the radio button. Here is what I have so far (I have some things in there that might not work because i'm trying to figure out what works): Code: <script type="text/javascript"> function getSelectedRadio() { len = document.dates.dep.length // returns the array number of the selected radio button or -1 if no button is selected if (document.dates.dep[0]) { // if the button group is an array (one button is not an array) for (var i=0; i<len; i++) { if (document.dates.dep[i].checked) { return i } } } else { if (document.dates.dep.checked) { return 0; } // if the one button is checked, return zero } // if we get to this point, no radio button is selected return -1; } function Calculate() { var i = getSelectedRadio(); if (i == -1) { alert("Please select if Marine entered Delayed Entry Program"); } else { if (document.dates.dep[i]) { return document.dates.dep[i].value; } else { return document.dates.dep.value; } } if (document.dates.yearDEAF.value < 1985 && document.dates.monthDEAF.value < 01 && document.dates.dayDEAF.value < 01 && first return from above ) { document.dates.yearDEP.value = (document.dates.yearDEAF.value); document.dates.monthDEP.value = (document.dates.monthDEAF.value); document.dates.dayDEP.value = (document.dates.dayDEAF.value); document.dates.yearPEBD.value = (document.dates.yearDEAF.value); document.dates.monthPEBD.value = (document.dates.monthDEAF.value); document.dates.dayPEBD.value = (document.dates.dayDEAF.value); } else if (document.dates.yearDEAF.value < 1985 && document.dates.monthDEAF.value < 01 && document.dates.dayDEAF.value < 01 && second return from above ) { document.dates.yearPEBD.value = (document.dates.yearAFADBD.value); document.dates.monthPEBD.value = (document.dates.monthAFADBD.value); document.dates.dayPEBD.value = (document.dates.dayAFADBD.value); document.dates.yearDEP.value = "N/A"; document.dates.monthDEP.value = "N/A"; document.dates.dayDEP.value = "N/A"; } } </script> I color coded in red the returns i'm trying to reference and where they need to be. Is this possible, and if so how can I do it? I haven't been able to find anything on the internet so far. Any help is greatly appreciated! Hi, i am very new to this place..so if i make nay mistakes, am sorry. and i am rookie at coding..so i may sound dumb... I wanted to know, if there is any method by which i can get the source code of a webpage in a program by using the URL. what i want to do is that am developing a javascript webpage which requires some info to be taken out of a certain webpage, and in the url of that webpage i can enter the query, so i wanted to know if there was any method to get the source of the webpage and store it in a varabile so i can scan it for the required info. Thanks. Hi, I would like to ask if there is anyway to get a variable from any webpage source using javascript. Something like javascript: document.getelement? Hey. I've been trying to get this page working with dynamically loaded iframe sources passed by URL, but I can't seem to get it working. Here's the relavent code: Code: <script type="text/javascript"> function delineate(str) { URL = str.indexOf("=") + 1; return(str.substring(URL)) } </script> Then there's a form/submit button that simply return the name of a type of item, for example "german" or "french" Using the form attributes method="link" and action="pageURL.html", where pageURL is the page that all of this is on. Code: <form name="hack"><input type="hidden" name="selector" /></form> <script language="javascript"> var url=window.location; document.hack.selector.value=url; var url_select=document.hack.selector.value; var selectedURL="../types/" +delineate(url_select) +".html"; document.write (selectedURL); frames['middleframe'].location.href=selectedURL; </script> <iframe id="middleframe" name="middleframe" src="../types/default.html" width="100%" scrolling="auto" height="1000" frameborder="0" style="display: inline;" /> I used the hidden form to change the url to text - advice from another website. I'm sure there are better ways to do it, but I'll explore them as soon as this thing actually works.. The types are in a subfolder named types, as shown. The document.write was placed in as a debug helper, and it displays the proper file location of whatever I select with the combo box, but the iframe doesn't change it's src to the selected page! I've tired the last line of the last <script> with several different methods, none of them producing any effect whatsoever. What am I doing incorrectly? Any help? Thanks. I want to hide my JavaScript but when I checked the FAQ of this forum, I saw that it said that it isn't possible. But how did worldometers.info did it? Because I cant find the JavaScript source. What is the code to stop people from viewing your source. Such as this site: http://bad-behavior.com (View The Source of That Site) I've searched the forums, and all I got was a bunch of "nothing can protect your source" I don't care about all of that. I want the code because: 1. Most people don't know how to view a source after it has been blocked 2. I think it's cool I am trying to use an external source for my javascript. The source loads with my script but the functions from the external source are not working. I do have it saved as a js file and in the same folder as my assignment. Below is my external source I am trying to use: <html> <body> <script type="text/javascript"> // Convert miles ==> kilometers function milesToKilos(miles) { return miles * 1.609; } // Convert kilometers ==> miles function kilosToMiles(kilos) { return kilos * 0.621; } // Convert miles ==> yards function milesToYards(miles) { return miles * 1760; } // Convert yards ==> miles function yardsToMiles(yards) { return yards * 0.0005682; } // Convert Fahrenheit ==> Celsius function fahrToCels(fahr) { return 5.0/9.0 * (fahr - 32); } // Convert Celsius ==> Fahrenheit function celsToFahr(cels) { return 9.0/5.0 * cels + 32; } // Convert Pounds ==> Kilograms function poundsToKilograms(lbs) { return 0.4536*lbs; } // Convert Kilograms ==> Pounds function kilogramsToPounds(kg) { return kg/0.4536; } </script> </body> </html> Now here is the javascript I am trying to use to run the external source. I can enter information but it is not using the functions from the external source. <html> <center><h2>Conversions</h2></center> <head> <script type="text/javascript" src="conversions.js"></script> </head> <body> <script type="text/javascript"> lbs = prompt ("Enter kilograms to convert", " "); var myFirstVariable ; myFirstVarible = "lbs"; alert (lbs) ; kg = prompt ("Enter pounds to convert", " "); var mySecVariable ; mySecVariable = "kg" ; alert (kg) ; </script> </body> </html> hi- trying to set frame source from javascript- i cant get it to work.. Code: <script> stud = mm_adl_API.LMSGetValue("cmi.core.student_id"); newlink = ('https://blah.blah.blah&stud_id='+stud); // --> </script> <frameset rows="*,80" frameborder="no" border="0" framespacing="0"> <frame src="javascript:window.location(this.newlink)" name="thisname" /> <frame src="bottom.html" name="bottomFrame" scrolling="No" noresize="noresize" id="bottomFrame" title="Scorm Handling" onload="mm_adlOnload()" /> </frameset> Is it possible to self source a javascript file? I have a file with code that opens a javascript window and document.writes to it. I want to run some code in this file attached to window.onload. Code: function someWindow() { var openedWindow = open(/* relevant vars */); openedWindow.document.write(/*doctype and html head markup*/) openedWindow.document.write("<script type=\"text/javascript\" src=\"(this file)\"></script>"); openedWindow.document.write("<script type=\"text/javascript\">window.onload = function(){ /* sourced code */}</script>") openedWindow.document.write(/*html body markup*/) openedWindow.document.close(); } then in the same file have code defining object constructors and such that attach events to various elements. I realize I can write a long string variable with all the code, but that would be visible a browser window -> view source window ( I know it won't hide the code in a separate source file) The object is to have a widget all in one file, rather than having a separate html, css file and another javascript code file. Thanks for advice, suggestions, encouragement, JK I am fairly new to JavaScript and I have been searching and searching and I cannot get a clear cut answer to my question. Here is what I am trying to accomplish: I want to gather the html source code from a particular sports score site and then store specifics of that resulting html (ie: the actual game scores) as strings, which I will then call in a windows sidebar gadget. Aside from actually getting the source code stored as a string, I would need to know how to then find a part of that string (ie: the text between "Last:" and "</") and store it as a separate variable. From what I have seen online I need to be using XMLHttpRequest... but I am completely lost when it comes to this. Can someone please give me a hand. Thanks in advanced. Please be as specific as possible. Dear all, I am trying to set up a web site for an art gallery. That is for new talents so I have no budget. I am not a web designer and I am facing some barriers. Maybe some of you could help me. My problem is, I guess, about going around with the DOM node tree. My page looks like this: FRAME 1 - Header FRAME 2 - Navigation bar FRAME 3 - Has arrows for scrolling iframe 3.1 ---- iframe 3.1 - has thumbnails FRAME 4 - First for query and after shows the enlarged pictures of thumbnails FRAME 5 - Has arrows for scrolling iframe 5.1 ---iframe 5.1 Has text explaining pictures in frame 4 How it works: In first place, after page has been loaded, I am using frame 4 to run a HTML form. After submit, iframe 3.1 is populated with thumbnails according to the query. When a thumbnail is clicked, I succeeded in bringing the corresponding text, talking about the artwork, on iframe 5.1 to do so, I change the html source in iframe 5.1 I am using for that: top.frames['frame5_id'].document.getElementById('iframe 5.1_id').src=new_source.html; The idea was about doing the same with the frame 4 that is for a larger picture of the artwork. But first I need to get rid of the form once it was submitted (bring a new HTML with a dummy transparent gif to be swapped with the desired picture). But how can I bring a new html in frame 4? After the form is submitted the 'action' moves to iframe 3.1 I tried changing the html source using getElementById but I could not find a way to get to the right object. So my questions a Two src attibutes must be changed with code written in iframe 3.1. (where the thumbnails are clicked). But... How can I address and change the HTML src code in FRAME 4 ? How can I address and change the src image in the new code loaded in FRAME 4 ? I have both attributes in variables but don't know how to get to the target elements Thanks for any help. regards, wagner I have these "editable" divs in my document. When you click them, a text box comes up and you type something and push enter. It changes what's in the div. There is also a blur event associated with the text box. When the user tabs or clicks away, it fires the blur event. Here lies the problem. I have other elements on the page that are clickable. I don't want those clickable things to fire their events if the user is clicking away from the text box, ie. the blur event. This isn't a problem is the user pushes tab, for instance. So my thought was that if i know how the user blurred (via keyboard, or clicking) i could take the appropriate action. Any ideas? |