JavaScript - Class Initialization && <script> Loader
Hello fellas,
First problem I want to initialize class php-like PHP Code: $i = 1; $name = 'myClass' . $i; $h = new $name(); How to do it in javascript ? Second If I'm adding a <script> to <head>, and in this new script there's a class named npcscript. How come right after adding the element, I can't initialize the class? Thanks for your time, nico Similar TutorialsHey all, I have a small function that replaces an image with that of a clicked thumbnail (and updates some text). Is it possible to display a loader whilst the new image is being downloaded? The code that I currently have is below. Thanks. Code: <script type="text/javascript"> //<![CDATA[ /** Sets the correct image in the central image container */ function getImage(src, title, content, width, height){ document.getElementById("post-attachment-title").innerHTML = title; if(content !== ''){ document.getElementById("post-content").style.display="inline"; document.getElementById("post-attachment-content").innerHTML = content; } else { document.getElementById("post-content").style.display="none"; } document.getElementById("post-attachment-image").src = src; document.getElementById("post-attachment-image").width = width; document.getElementById("post-attachment-image").height = height; } //]]> </script> I'm in the process of building a blog that will be using some pretty neat jQuery effects to spotlight its content. One thing I've been looking at doing is trying to lessen the amount of different pages the visitor has to load by using things like sliders and accordions to hide and display content. Overall I'm not concerned that the pages will take more than 3-4 seconds on any decent connection to load, however I was thinking about visitors who are potentially running slower connections. To avoid losing those slower-connected users I've been looking for a way to display a page loader, much like what you see on flash websites, except completely without flash. I was hoping there might be some AJAX algorithm or jQuery function I wasn't aware of that might help me accomplish this. If anyone could point me in the direction of a tutorial, I'd really appreciate it! Thanks in advance. The script inside my opening if statement is working, but is the rest of this written right? I'm just want my script to run after it makes sure that a div class named single-journal-entry-wrapper is on the page. Code: function doit() { document.getElementsByTagName("div") for ( var x = 0; x < div.length; x++ ) { var div = div[x]; if ( div.className.indexOf("single-journal-entry-wrapper") >= 0 ) { do somethang!} } } { Hello, I'm building a simple custom dropdown menu for a web-site so that I can control look and feel as well as the behavior of an iframe. I've set it up so that when the user clicks on any of the menu's active components, an "if" sequence in the clickHandler() function is activated that gets the element's class. If the event occurred on a dropdown option, the event-target id is checked, and the appropriate function is called. At least this is how it's supposed to work. All of the drop-down options have been assigned the "dmo" class. When I test the menu and click on other elements, it works fine. But when I click the "dmo" drop-down options, nothing happens. Firebug and IE Explorer Developer Tools both show no errors, but on clicking a dropdown option, the correlate code doesn't run at all, and after hours of head-banging, I have no idea why. I need help on this one. Here's the code. Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <style type="text/css"> BODY { background:#000; } .DropMenu { width:228px; height:1.5em; } .DropMenu span { color:#FFF; font-size:0.875em; font-family:Verdana, Geneva, sans-serif; font-weight:bold; } .DropMenuLabel { float:left; width:85px; height:1.5em; padding:0.2em 0 0 0; text-indent:0.3em; } .DropMenuTopList { width:130px; float:left; height:1.5em; padding:0 0 0 0; cursor:pointer; } .DropMenuTopList img { padding:0.4em 0 0 0.3em; cursor:pointer; } .DropMenu span { color:#FFF; font-size:.875em; font-family:Verdana, Geneva, sans-serif; font-weight:bold; vertical-align:center; } #dropMenuButton { cursor:pointer; } #dropMenuOptions { position:relative; width:130px; left:85px; padding:0.2em 0 0 0; visibility:hidden; cursor:pointer; } .MenuList { list-style-type:none; top:0; left:0; width:100%; margin:1.5em 0 0 0; padding:0; } .MenuList li{ height:1.5em; margin:0.2em 0 0 0; padding:0; width:100%; background-color:#666; border-color:#AAA; border:0.5px solid; text-indent:0.3em; } .dmo { z-index:100; } </style> <script type="text/javascript" language="javascript"> window.onload = function() { setBehavior(); } function setBehavior() { /* var downButton = document.getElementById('dropMenuButton') downButton.onclick = new Function ( "showMenu()" ); */ window.onclick = new Function ( "evt" , "clickHandler( evt )" ); } function clickHandler( evt ) { var dropMenu = document.getElementById('dropMenuOptions'); var e = evt; var targ; if (e.target) {targ = e.target} else if (e.srcElement) {targ = e.srcElement} var targClass = targ.getAttribute('class'); /* classnames dmb - dropdown button - this should toggle the dropdown visibility on and off on click, and toggle visibility on over/out dmo - option - once clicked, this should turn the dropdown visibility off */ var exclude = ("dmb" || "dmo") if (targClass != exclude ) { hideMenu() } else if (targClass == "dmb" ) { if (dropMenu.style.visibility == "visible") { hideMenu() } else showMenu(); } else if (targClass == "dmo" ) { if ( targ.id == "option1" ){ window.open(www.bing.com,"Bing") } else if ( targ.id == "option2" ){ window.open(www.bing.com,"Bing") } else if ( targ.id == "option3" ){ window.open(www.cuil.com,"Cuil") } hideMenu(); } } function showMenu() { var dropMenu = document.getElementById('dropMenuOptions'); dropMenu.style.visibility= "visible"; } function hideMenu() { var dropMenu = document.getElementById('dropMenuOptions'); dropMenu.style.visibility= "hidden"; } </script> </head> <body> <div id="customMenu" class="DropMenu"> <span> <div class="DropMenuLabel">Options:</div> <div class="DropMenuTopList dmb" id="dropMenuTopList">Temp Text <img class="dmb" id="dropMenuButton" src="Assets/Buttons/DropMenuDownButton.png"> </img> </div> <div class="dmo DropMenuOptions" id="dropMenuOptions"> <ul id="menuList" class="dmo MenuList"> <li id="option1" class="dmo">Option1</li> <li id="option2" class="dmo">Option2</li> <li id="option3" class="dmo">Option3</li> </ul> </div> </span> </div> </body> </html> http://colorschemedesigner.com/ If you're on fast internet, you might not even see it, but I don't know how they do two things upon entrance. 1.) Allow the content to load (and function as a preloader) 2.) Get everything to fade in after loader completes. Is it just a div overlapping everything where opacity turns from 1 to 0 or what? I know this loader must be javascript, but this is all I can find when viewing the source, so I don't know if there is php involved but I'm guessing there is. Code: <div id="csd3-jscheck"> <h1>Color Scheme Designer 3</h1> <div id="csd3-load"> <h4>You need Javascript & CSS to run this application.</h4> <p>You may also try <a href="/previous">previous versions</a>.</p> <hr> </div> </div> Code: <script type="text/javascript"> <!-- var elm = document.getElementById('csd3-load'); if (elm) elm.innerHTML = '<p>Wait please. Loading...<'+'/p>'; //--> </script> Code: #csd3-jscheck { margin: 20%; padding: 2em 4em; border: 1px dotted #666; text-align: center; } #csd3-jscheck hr { display: none; } Hello, I am new with Javascript and running into this problem that I don't understand. I define a base class that only contains an array "elements", and a derived class that contains nothing more (for simplicity): Code: function baseClass() { this.elements = new Array; } function derivedClass() { } derivedClass.prototype = new baseClass; Then I create two instances of the derived class, and add two elements in each: Code: a = new derivedClass(); a.elements.push("A"); a.elements.push("B"); b = new derivedClass(); b.elements.push("C"); b.elements.push("D"); When I examine the contents of these arrays, I see that both a.elements and b.elements contain {"A","B","C","D"} ! I.e. as if the two arrays are in fact the same array! But these are two separate instances of the class, I expect two separate arrays. Note that if instead I use the base class: Code: a = new baseClass(); a.elements.push("A"); a.elements.push("B"); b = new baseClass(); b.elements.push("C"); b.elements.push("D"); then I get a.elements = {"A","B"} and b.elements = {"C","D"}, as I would expect. Could someone explain to me the problem with using the derived class? Thank you, Stephanos Hi All, I have two scripts which I want to try and integrate. I am using a nice gallery script to show thumbnails which are appended to a an image wrapper which on click of the thumbnail shows the larger image in the image wrapper, I am trying to implement cloud zoom which is a plugin which uses image srcs to then point to an anchor href to show another larger zoom image either in the same place.. which is what I am trying to do or in another div beside. I have had to set me img srcs up in a certain way to easily enter some product details. and I need to try an manipulate the code to make it work to suit my file layout. I am using a var= images [ with a series of file locations and info such as below { src: 'romanticabride/thumbs/tn_Shauna.jpg', srcBig: 'romanticabride/images/Shauna.jpg', title: 'Shauna', longDescription: '<b><H1>Shauna</H1></b><br><b>Romantica Of Devon <br><br><h2>Sizes Available:</h2><br> 6 - 32.<b><br><b><br><b><b><b><H2>Colours Available:</h2><b><br>Various<br>Please Enquire Below<br><br><br><br><a href="mailto:tracy@cherishbridal.co.uk?subject=Web Enquiry Regarding Romantica Shauna Bridal Gown"class="enquiry rose glow" >Click To Enquire About This Item </a>' }, what I need is for cloud zoom to work when the main image wrapper is hovered over which means it will need to add a class or when the whichever srcBig: is hovered over it gets wrapped by href to make the script work . one of my pages is http://www.cherishbridal.co.uk/romaticabride.html the cloud zoom script is at http://www.professorcloud.com/mainsite/cloud-zoom.htm.. I am happy to share a jsfiddle with someone or explain further or post some code. Thank you in advance does any expert know how to pass parameters in the <script ..> tag? for instance; Code: <script type="text/javascript" src="script.js ?param1=val1¶m2=val2&etc "> in the javascript script.js, how would we read the params after the question mark? for example, google this; google shopping cart /v2_2/cart.js I have a script that works in seamonkey(my html editor) but when I use it in IE8 it says errors happen. Here's the code (the first line is on line 7 of the html file): Code: <script type="text/javascript"> function enlarge(imageNum) { var numToString = ""; if(parseInt(imageNum) < 10){ numToString = "0" + imageNum; } else { numToString = imageNum + ""; } window.open("images/LgScreenshot"+numToString+".jpg","Screenshot "+imageNum,"status=0,height=675,width=900,resizable=0"); } </script> And the errors: Webpage error details User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; GTB0.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; InfoPath.3; .NET CLR 3.0.30729) Timestamp: Wed, 10 Feb 2010 14:58:16 UTC Message: Object expected Line: 150 Char: 1 Code: 0 URI: http://samssc2site.co.cc/Features.html Message: Invalid argument. Line: 18 Char: 1 Code: 0 URI: http://samssc2site.co.cc/Features.html I need to assign a key in the javascript to actually make the javascript work,. I have a bookmark in chrome , a javascript , which actually works when clicked on it .,. but how can i edit it so that i can actually make it work on click a key or combination of keys. i want to declare the key or keycombo in the script itself .,. the script is for catching the selected text on the webpage and opening a new tab(or window) and doing an exact search search of the selected text using google.com .,., So I want it to work it this way ., select the text press a key and it opens a new tab (or window) with an xact search .,. i want to declare the key or keycombo in the script itself .,. the script is for catching the selected text on the webpage and opening a new tab(or window) and doing an exact search search of the selected text using google.com .,., So I want it to work it this way ., select the text press a key and it opens a new tab (or window) with an xact search .,. Thanks in advance ., Nani On this website: http://evancoleman.net/index.php I really like on the top how the menu has like 5 or 6 icons, and when you hover over them it shows a bubble with the name in them. Does anybody know where I can find this script? Thanks. Hi all, On my site each page is composed by header, contents and bottom. Each part, located respectively at header.php, content1.php, content2.php, ... and bottom.php, is included in page.php. I have a menu, that is the same for all pages, on header.php: Code: <ul class="site-nav"> <li><a href="home.php" class="" name="Home"></a></li> <li><a href="page2.php" class="" name="OK"></a></li> </ul> I'd like to set a class in order to have its corresponding menu with this class="act" for each content.php. I tried this but it doens't work: I add this in content1.php Code: <!-- act for site-nav --> <script language="javascript" type="text/javascript">function changeClass() { document.getElementByName("Home").setAttribute("class", act); } </script> and this in top.php Code: <ul class="site-nav"> <li><a href="home.php" class="changeClass()" name="Home"></a></li> <li><a href="page2.php" class="changeClass()" name="OK"></a></li> </ul> Please help me, I don't understand anything of javascript . So, I have this javascript that works just fine... But... PHP Code: <script language="javascript"> function addBtn() { element = document.createElement("input"); element.setAttribute("type", 'button'); element.setAttribute("value", 'Track'); element.setAttribute("name", 'element'); element.onclick = function() { alert("Tracking"); }; var btn = document.getElementById("gs-track-btn"); btn.appendChild(element); } </script> <div id="gs-track-btn"></div> How would I target that div if the " gs-track-btn " id was a class? hey I want to get the href via attributes of this a tag & then this href to be set to a variable Hi, I need to change my class, but this isn't doing it for me and I don't see why. No error messages, but nothing happens. Code: .myClass { background-color: yellow; } document.getElementById("myId").setAttribute("class","myClass"); Thank You in Advance for Your Help, Lou Hello everybody. For the moment im locking for a JavaScript-document. That will be working like this... When we are push on a link, for example to username. So will it coming up a box "<div class="profile_class"></div> From to beeing visibility: hidden; display: none; To be visibility: visibile; display: block; .profile_class { float: left; width: 300px; height: 250px; margin-top: 2px; margin-left: 2px; position: absolute; visibility: hidden; display: block; background-color: #151515; } .profile_class_inactive { visibility: visible; display: none; } When we are push on the mouse outsite the "profile_class" will it beeing closing/invisbile. But when we have it active something like "profile_class" so will it show uz... <ul> <li>Send a message</li> <li>Add contact</li> <li>Block the contact</li> </ul> And on the right site, will it beeing showing uz a little avatar. Can you please help me with this one? I'm a amateur and I've just coded this little script. What it does is quite simple, once you click on the 'theBox' div, it switches from one class to another. I can't help having the feeling that this could have been coded in a lot more simple way than the way I've coded. So I thought that maybe you guys could help me out. (Oh, and by the way, the script works fine in FireFox and IE). Code: <script> function setDivClassName(string){ document.getElementById("divClassName").innerHTML = string; } function theBoxOnClick(){ var classes = new Array(); classes[0] = 'zero'; classes[1] = 'one'; classes[2] = 'two'; var currentClassName = document.getElementById('theBox').className; var currentClassIndex; var nowGoTo; for(var i = 0;i <= classes.length-1;i++){ //alert(classes[i]); if(classes[i] == currentClassName){ currentClassIndex = i; nowGoTo = currentClassIndex + 1; } } if(classes[nowGoTo] == undefined){ nowGoTo = 0; } document.getElementById('theBox').className = classes[nowGoTo]; setDivClassName(classes[nowGoTo]); } </script> <style> .zero{width:100;height:100;background-color:black;color:white;padding:10;border-style:solid;border-color:red;border-width:2} .one{width:100;height:100;background-color:yellow;color:red;padding:10;border-style:solid;border-color:red;border-width:2} .two{width:100;height:100;background-color:red;color:yellow;padding:10;border-style:solid;border-color:red;border-width:2} </style> <body onLoad="setDivClassName('zero')"> <div id="theBox" class="zero" onClick="theBoxOnClick()"> This DIV's Class is <div id="divClassName"></div> </div> Alright I have never really created a class before and I have to for a project. This is what I have so far and I will try to explain the requirements I am looking for thoroughly and what problems I am having. Code: <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <script language="javascript" type="text/javascript"> function clock () { //Object Constructor this.setT = function() { //Method Set Time //Properties this.currentTime = new Date ( ); this.currentHours = this.currentTime.getHours ( ); this.currentMinutes = this.currentTime.getMinutes ( ); this.currentSeconds = this.currentTime.getSeconds ( ); } clock.prototype.displayTime = function () { clock.setT } } </script> <body> <form name="form1"> <p> <input type="text" rows=7 cols=60 name="currentTime"> <br> <input type="button" value="Set Time" onClick="clock.displayTime();"> <br> <input type="button" value="Display Time" onClick="currentTime.value= completeTime;"> <br> <input type="reset" value="Reset"> </p> </form> </body> </html> The clock class contains: Three Properties: Hours Minutes, Seconds Two Methods setTime (which uses date class to get current time) Must be defined in the constructor displayTime (returns a string displaying current time) Must be defined as a prototype property of constructor The below is a very rough outline, and probably not correct. I've been reading up on objects for the last few hours and can't be very sure of anything yet. Code: function clock() { this.currentTime = new Date ( ); var currentHours = currentTime.getHours ( ); var currentMinutes = currentTime.getMinutes ( ); var currentSeconds = currentTime.getSeconds ( ); this.setTime = function() { } } clock.prototype.displayTime = function ( ) { } I guess my biggest problem now is the outline. Any help would be great hopefully once i get what goes where i can actually start writing the code. I already have the full code to write a basic clock just like this but it doesn't use classes or any of this. I have a store that sells certain products. When you add a product to the cart and go to view your cart you can estimate shipping costs. It will list different shipping methods and the cost to ship them. However, there is one shipping method called "Request a Quote" that has a price value of 0.01. This can confuse customers because they sometimes think that the shipping is only $0.01 when it really isn't. So I want to just remove this value, but still list pricing for all the other shipping methods. I know I can just do this using CSS and setting the display to none. But am having trouble getting it to work. Here is the link to the shopping cart page: Folding Chairs and Tables.com - Shopping Cart If you add a product and run the 'estimate shipping and tax' you will see what I'm talking about. How can I remove this value? Any help would be really appreciated. Here is a picture of what the table looks like when you calculate the estimate shipping costs: Thank You If in my page I have a facebook widget & a pagecounter widget, both I dont want to display on pages other than homepage. I will do 1] Code: <div id="facebook"> .....facebookwidget...</div> <div id="counter">.....counterwidget.....</div> then I will use something like this 2] Code: $(function() { var currentUrl = window.location.href; if(currentUrl != 'http://engineering.forumotion.info/') { $('#facebook').remove(); $('#counter').remove(); } }); However if I use a same id for both widgets say e.g facebook then Code: $(function() { var currentUrl = window.location.href; if(currentUrl != 'http://engineering.forumotion.info/') { $('#facebook').remove(); } }); the above code only removes one(that occurs first) widget & NOT the other widget with the same id. Here we include looping, plz anyone tell me how one start & end a loop, to find a particular class/ID. Plus how the jquery .each() function can be implemented |