JavaScript - Onload Cookie Problems
Hi I am building a site using a cookie to gather name and then insert into a form on the site later. The problem I am having with the
Code:
body onload="checkCookie(); "> however my DHTML nav menu will not load afterwards. here is the code O have. Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>American Cancer Society | Relay For Life</title> <link rel="stylesheet" href="style.css"> <script type="text/javascript" src="navigation.js"></script> <script type="text/javascript" src="cookie.js"></script> </head> <body onload="checkCookie(); "> <div id="mainContainer"> <a href="images/ACS Logo.jpg"><img src="images/ACS Logo.jpg" alt="ACS Logo" width="462" height="165"></a> <div id="navigation_menu"> <ul> <li><a href="index.html">ACS Relay For Life</a></li> <li><a href="Find_Event.html">Find An Event</a> <ul> <li><a href="http://www.relayforlife.org/statesville">Statesville</a></li> <li><a href="http://www.relayforlife.org/mooresville">Mooresville</a></li> <li><a href="http://www.relayforlife.org/getInvolved/FindanEvent/app/findanevent.aspx">A Different Event</a></li> </ul> </li> <li><a href="About_Relay.html">About Relay</a> <ul> <li><a href="mission.html">Mission</a></li> <li><a href="money.html">Where Does The Money Go?</a></li> <li><a href="help.html">How Can I Help?</a></li> </ul> </li> <li><a href="Media.html">Media</a> <ul> <li><a href="Photos.html">Photos</a></li> <li><a href="Videos.html">Videos</a></li> </ul> </ul> </div> Here is the cookie.js file Code: function getCookie(c_name) { var i,x,y,ARRcookies=document.cookie.split(";"); for (i=0;i<ARRcookies.length;i++) { x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("=")); y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1); x=x.replace(/^\s+|\s+$/g,""); if (x==c_name) { return unescape(y); } } } function setCookie(c_name,value,exdays) { var exdate=new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); document.cookie=c_name + "=" + c_value; } function checkCookie() { var username=getCookie("username"); if (username!=null && username!="") { alert("Welcome again " + username); } else { userName = prompt("What is your name?", "Enter your name here"); if (username!=null && username!="") { setCookie("username",username,365); } } } Similar TutorialsHello Can anyone help with the following problem On the following page http://www.stroudskittles.co.uk/signingon.html I have a Body onload event (to load the chained Menu) and a windows.onload event (to load the IFrame Ticker) If I open the page in Internet explorer both scripts function; however in Firefox I'm having problems. The Chained Menu works, however the IFrame Ticker does not display. Both events ahave ben placed on the boady tag. Can anyone advise how I can get both to display in Firefox Can anyone Hi everyone, I am using a jQuery cookie script to set the cookie of some elements on my website. One of the problems is that I need the cookie to not expire after one day, I need it to expire after a while (I'm going to start off with a year). Here's my script, the red part is what I've been editing. Code: /** * Cookie plugin * * Copyright (c) 2006 Klaus Hartl (stilbuero.de) * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * */ /** * Create a cookie with the given name and value and other optional parameters. * * @example $.cookie('the_cookie', 'the_value'); * @desc Set the value of a cookie. * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secu true }); * @desc Create a cookie with all available options. * @example $.cookie('the_cookie', 'the_value'); * @desc Create a session cookie. * @example $.cookie('the_cookie', null); * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain * used when the cookie was set. * * @param String name The name of the cookie. * @param String value The value of the cookie. * @param Object options An object literal containing key/value pairs to provide optional cookie attributes. * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object. * If a negative value is specified (e.g. a date in the past), the cookie will be deleted. * If set to null or omitted, the cookie will be a session cookie and will not be retained * when the the browser exits. * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie). * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie). * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will * require a secure protocol (like HTTPS). * @type undefined * * @name $.cookie * @cat Plugins/Cookie * @author Klaus Hartl/klaus.hartl@stilbuero.de */ /** * Get the value of a cookie with the given name. * * @example $.cookie('the_cookie'); * @desc Get the value of a cookie. * * @param String name The name of the cookie. * @return The value of the cookie. * @type String * * @name $.cookie * @cat Plugins/Cookie * @author Klaus Hartl/klaus.hartl@stilbuero.de */ jQuery.cookie = function(name, value, options) { if (typeof value != 'undefined') { // name and value given, set cookie options = options || {}; if (value === null) { value = ''; options.expires = -1; } var expires = ''; if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { var date; if (typeof options.expires == 'number') { date = new Date(); date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000 * 365)); } else { date = options.expires; } expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE } // CAUTION: Needed to parenthesize options.path and options.domain // in the following expressions, otherwise they evaluate to undefined // in the packed version for some reason... var path = options.path ? '; path=' + (options.path) : ''; var domain = options.domain ? '; domain=' + (options.domain) : ''; var secure = options.secure ? '; secure' : ''; document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); } else { // only name given, get cookie var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } }; Hi all I would like the img src to be populated after page load I'm not sure this is doing what I would like it to do. I have done this. (tag shortened for post) Code: <img src="" id="testImg2" onload="(this.src='images/USA.jpg')"/> Is this populating the img src after page load or when the img tag is loaded? LT Hi, I'm looking to create a box that shows to IE6 users or lower, to tell them that my site does not render properly in the IE6 browser and that they should therefore upgrade to a newer version or alternative browser. Basically I would put whatever the code to show the box into these tags: Code: <!--[if lte IE 6]> INSERT HERE <![endif]--> to enable it to only appear to IE6 or below users. Preferably when this box appeared, it would be nice if it could fade the page behind it. Does anyone have a script for this or know of a tutorial I could use? The box would also need buttons at the bottom that said "Ok, upgrade me" (this would load another window with links to alternative browsers) or "No thanks" (or "Close") Thanks in advance. I have this code: Code: <script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4c7b22470e521876"></script> I want this JavaScript code above to be executed when all My page finished loading (maybe by using JavaScript onload) So how can I do that? Am I saying the onload event wrong or something? PHP Code: <input type="button" value="Mouse" onload="popup_show('popup', 'popup_drag', 'popup_exit', 'mouse', -10, -10);" /> Other events like onmouseover and onClick work Hi all, I have some javascript that when a link is clicked hides the div and when it is clicked again shows the div... what I want to do is reverse that, so it is intially hidden. I have tried changing the display none to different parts but it always shows on load: Code: function toggleDiv (divid) { if (document.getElementById (divid).style.display == 'none') { document.getElementById (divid).style.display = 'block'; } else { document.getElementById (divid).style.display = 'none'; } } Code: <a href="javascript:;" class="showmorelink" onmousedown="toggleDiv('checks');">Safeguarding</a> Can anyone advise? Many thanks, Greens85 Hi, I got some JavaScript code from the net to resize DIV's depending on the window-size. It works perfectly in FF, but it won't work in IE. Code: function Resize(){ var winW = 630, winH = 460; if (parseInt(navigator.appVersion)>3) { if (navigator.appName=="Netscape") { winW = window.innerWidth-30; winH = window.innerHeight-200; } if (navigator.appName.indexOf("Microsoft")!=-1) { winW = document.body.offsetWidth-30; winH = document.body.offsetHeight-200; } } var topHeight = document.getElementById('top').offsetHeight; var setHeight = document.getElementById('settings').offsetHeight; var headersHeight = document.getElementById('headers').offsetHeight; var leftWidth = document.getElementById('left_column').offsetWidth; document.getElementById('container').style.width=winW+"px"; document.getElementById('container').style.height=(winH-topHeight-setHeight)+"px"; document.getElementById('headers').style.width=(winW-leftWidth)+"px"; document.getElementById('data').style.width=(winW-leftWidth)+"px"; document.getElementById('data').style.height=(winH-topHeight-setHeight-headersHeight)+"px"; } I've been working on this for some days. I would appreciate any help I can get. Erik I have a popup window that calls a texteditor. I want the texteditor to show the inner HTML of the parent doucument by div id. When I call the function through a submit (in child window) it workks great, but I want innerHTML to be added when child page loads. I have tried calling function in body onLoad=getparent; tries adding it to inner script where it calls the editor , again onLoad=getparent; Tried with and without () i.e onLoad=getparent(); Child window code: [code] <html> <head> <title>Mangae Content</title> <script type="text/javascript" src="/ckeditor/ckeditor.js"></script> <script language="javascript"> function getparent() { var TextToPass = window.opener.document.getElementById('missionStatement'); var TextToEdit = TextToPass.innerHTML; document.getElementById('editor1').value=TextToEdit; } </script> </head> <body> <form method="post" name="editedcontent"> <p> My Editor:<br /> <textarea name="editor1" id="editor1"></textarea> <script type="text/javascript"> onload=getparent; CKEDITOR.replace( 'editor1' ); </script> </p> <p> <input type="submit" onClick="getparent();"/> </p> </form> </body> </html> [code] When I use the submit button it works fine. Captures parent window HTML and loads it into ckeditor, How do I get it to load without using the submit button??? Any help would be greatly appreciated. Hi Guys, I want to call the below when the page opens as opposed to clicking a link How do I alter it and do i need a function? THANKS <a href="#" onclick="new Lightbox.base('box3', { closeOnOverlayClick : true })">LINK hi guys, how can i get this code to load on body onload? without fading in and out? Code: <html> <head> <!-- For ease i'm just using a JQuery version hosted by JQuery- you can download any version and link to it locally --> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> $(document).ready(function() { $("#responsecontainer").load("response.php"); var refreshId = setInterval(function() { $("#responsecontainer").load('response.php?randval='+ Math.random()); }, 9000); $.ajaxSetup({ cache: false }); }); </script> </head> <body> <div id="responsecontainer"> </div> </body> Thanks Hi there, I'm trying to port a pretty old javascript to a brand new Joomla website. I didn't create the original site that the javascript comes from, and haven't been able to locate it in my Google searches either. I'm assuming it's quite old. That being said, it seems to *want* to load up correctly. Since this is in an individual Joomla article, I didn't want to set the template to always try and load this script from <body>, so I'd found what I thought might be a workaround by calling it from another javascript within the article. It *looks* like it starts to want to call the required images, but then they disappear. I am not a javascript programmer in any way. I really don't do more than copy & paste various scripts of that nature. So I'm a little flummoxed here. Any help you can afford will be appreciated. Here is the development page: Mixes Thanks in advance. ~Laura rethink
I am using this code Code: <BODY onload="window.top.location='http://localtimes.info/clock.php?cp3_Hex=0F0200&cp2_Hex=FFFFFF&cp1_Hex=000080&fwdt=118&ham=0&hbg=0&hfg=0&sid=&mon=&wek=&wkf=&sep=&continent=Asia&country=Indonesia&city=Jakarta&widget_number=116'"> but the problem is that when the page containing this code loads, it redirects Me to the page (a new page) of the JavaScript code, So how to make this JavaScript code load on the same page and not redirecting Me to a New page? (and make it load when the all page finish loading) Hei, On my website , I have certain things that happen in the function window.onload. I need them to happen only the first time when the user acceses the page . Do you have any idea how to do this? Thanks while decomplicating things I putted into single jsp code like that: Code: <script language="javascript" src="../js/A.js" type="text/javascript"> </script> <script language="javascript" src="../js/B.js" type="text/javascript"> </script> The prob here is that both A and B contains window.onload, and only the one from A gets executed. All things that come to my mind is back to complications, loosing track of things. Any advice ? Hi everyone, I am at the last leg of getting my code to run. I have a window.onload function, but right now it is working only when I fill in all the field in the drop down list, hit a refresh and then hit submit button. What changes should I implement in the code? The code after the window.onload function runs as follows: Code: window.onload = function() { age = 0; groom_age = getvalues(); caste = 0; groom_caste = getvalues_1(); profession = 0; groom_profession = getvalues_2(); bachelor = 0; groom_bachelor = getvalues_3(); master = 0; groom_master = getvalues_4(); doc = 0; groom_doc = getvalues_5(); country = 0; groom_country = getvalues_6(); color = 0; groom_color = getvalues_7(); height = 0; groom_height = getvalues_8(); marriage = 0; groom_marriage = getvalues_9(); father = 0; groom_father = getvalues_10(); switch(groom_age) { case "23" : age = 1.6; break; case "24" : age = 1.2; break; case "25" : age = 1.7; break; case "26" : age = 1.8; break; case "27" : age = 1.9; break; case "28" : age = 1.75; break; case "29" : age = 1.7; break; case "30" : age = 1.4; break; case "31-35" : age = 1.8; break; case "36-40" : age = 1.9; break; case "41-45" : age = 1.75; break; case "46-50" : age = 1.7; break; case "51+" : age = 1.4; break; default: age = 1; } switch(groom_caste) { case "Brahmin" : caste = 1.6; break; case "Bania" : caste = 1.2; break; case "Kayastha" : caste = 1.6; break; case "Kshatriya" : caste = 2.0; break; case "Dalit" : caste = 1.8; break; case "Maravar" : caste = 1.6; break; case "Mudaliar" : caste = 2.0; break; case "Jat" : caste = 1.8; break; case "Labanas" : caste = 1.6; break; case "Rajput" : caste = 1.6; break; default : caste = 1; } switch(groom_profession) { case "Engineer" : profession = 1.6; break; case "Doctor" : profession = 1.2; break; case "Lawyer" : profession = 1.65; break; case "CA" : profession = 1.7; break; case "IAS" : profession = 2.0; break; case "Family Business" : profession = 1.5; break; case "Engineer + MBA" : profession = 1.95; break; case "None of the above" : profession = 1.95; break; default: profession = 1; } switch(groom_bachelor) { case "BTech/BE/BS" : bachelor = 1.6; break; case "MBBS" : bachelor = 1.2; break; case "BA" : bachelor = 1.1; break; case "B.Com" : bachelor = 1.4; break; case "BSc" : bachelor = 1.9; break; case "BBA" : bachelor = 1.1; break; case "Diploma" : bachelor = 1.4; break; case "Certification Course" : bachelor = 1.4; break; default: bachelor = 1; } switch(groom_master) { case "MS" : master = 1.6; break; case "MBA" : master = 1.2; break; case "MHA" : master = 1.4; break; case "MTech" : master = 1.4; break; case "M.Ed" : master = 1.4; break; case "No Master degree" : master = 1.4; break; default: master = 1; } switch(groom_doc) { case "Doctor of Philosophy (Phd)" : doc = 1.6; break; case "Doctor of Medicine (MD)" : doc = 1.2; break; case "No Doctorate Degree" : doc = 1.4; break; default: doc = 1; } switch(groom_country) { case "India" : country = 1.6; break; case "USA" : country = 1.2; break; case "Any European Country" : country = 1.7; break; case "Australia" : country = 1.7; break; case "Canada" : country = 1.7; break; case "Any Country more developed than India" : country = 1.7; break; case "Any Country less developed than India" : country = 1.2; break; default: country = 1; } switch(groom_color) { case "Fairy White" : color = 1.6; break; case "Black" : color= 1.2; break; case "Wheatish (Almost White. Would need some Fair n Lovely)" : color= 1.4; break; case "Brown": color= 1.4; break; case "White": color= 1.9; break; case "Pitch Black (Not visible on a moonless night)" : color= 0.6; break; default: color = 1; } switch(groom_height) { case 'Less than 5\'4"' : height = 1.6; break; case '5\'4"' : height = 1.2; break; case '5\'5"' : height = 0.8; break; case '5\'6"' : height = 1.0; break; case '5\'7"' : height = 1.4; break; case '5\'8"' : height = 1.5; break; case '5\'9"' : height = 1.6; break; case '5\'10"' : height = 1.7; break; case '5\'11"' : height = 1.8; break; case '6' : height = 1.85; break; case '6\'1"' : height = 1.9; break; case 'Greater than 6\'1"' : height = 1.8; break; default: height = 1; } switch(groom_marriage) { case "0" : marriage = 1.6; break; case "1" : marriage = 1.2; break; case "2" : marriage = 0.4; break; case "More than 2" : marriage = 0.2; break; default: marriage = 1; } switch(groom_father) { case "Doctor" : father= 1.6; break; case "IAS" : father = 1.2; break; case "Engineer" : father= 1.7; break; case "Lawyer" : father= 1.65; break; case "CA" : father= 1.8; break; case "Engineer + MBA" : father= 1.9; break; case "Family Business" : father= 1.2; break; default : father = 1; } }; function processOrder() { var total = age + caste + bachelor + master + doc + color + profession + father + marriage + height + country; var pageNumber = Math.floor((20 - total) / 2 + 1); // pageNumber = 6; // alert(pageNumber); window.open ("Page " + pageNumber + ".html"); } </script> <center> <INPUT TYPE="button" VALUE="Calculate" onClick="processOrder()"> </center> I would really appreciate any kind of help. Thanks a lot! Hi, I'm working on a small bit of code that passes a logged in users email address and name to a live chat form (Zompim & WHMCS) http://api.zopim.com/#setName By striping the code from the above page, I've managed to get it to work when the link is clicked, but I needed to have the variables passed upon page load. If anyone can point me in the right direction that would be great, me and Javascript don't work too well together! Here is what I have so far http://myautodj.com/zomp_test.php (your request a chat, and click on the run script link - that passes the demo email and name) Here is the code Code: <pre><script> $zopim.livechat.set({ language: 'en', name: 'Visitor', email: 'visitor@site.com', onStatus: function(s) { alert(s) } }); </script></pre> <script> (function() { var pres = document.getElementsByTagName('pre'), i; for (i=0; i<pres.length; i++) { buildLiveExample(pres[i]); } function buildLiveExample(p) { var code = p.innerHTML.replace(/>/g, '>').replace(/</g, '<'); var div = document.createElement('div'); if (code.search('<script>') == 0) { var a = document.createElement('a'); a.innerHTML = 'run script'; a.href = '#'; a.onclick = function() { eval(code.replace(/<\/?script>/g, '')); return false; }; div.appendChild(a); } else { div.innerHTML = code.replace(/<script>[\s\S]*?<\/script>/g, '') var code_blocks = code.match(/<script>[\s\S]*?<\/script>/g); if (code_blocks) { for (var i=0; i<code_blocks.length; i++) eval(code_blocks[i].replace(/<\/?script>/g, '')); } } p.parentNode.insertBefore(div, p); } })() </script> <script type="text/javascript"> document.write(unescape("%3Cscript src='" + document.location.protocol + "//zopim.com/?4vXfkefMFbxeWAVPjvRvWnBodfrGzctq' charset='utf-8' type='text/javascript'%3E%3C/script%3E")); </script> Hi I am in need of some help with my Javascript code, basicly I am creating a Javascript SDK for my site and I am trying to make is a little like jQuery in which you check if the document is ready such as $('document').ready(function(){}); which my SDK looks like this $.functions.push(function(){}); the only thing is when using it like this each function push gets added to the functions array which i then need to use on the documents load. With my SDK I use $.onload.ready();, which should act as window.onload and run all the functions with-in the array but I cant seem to get it to work. This is what I currently have. Code: $.functions =new Array(); $.onload = {}; $.onload.ready = function(){ if(window.onload !== undefined && window.onload != '') { window.onload=function(){for(var i=0;i<$.functions.length;i++){$.functions[i];}}; } }; Can anyone help me out? Thank You DJCMBear EDIT: Don't worry I have found the solution and it works! This is the code for the onload. Code: $.onloadAdd = function(func) { if(typeof onload == 'function') { var oldonload = onload; onload = function() { oldonload(); func(); } } else { onload = func; } }; $.onload = function(){ if(window.onload !== undefined && window.onload != '') { for(var i=0;i<arguments.length;i++) { $.onloadAdd(arguments[i]); } } }; And this is the onload functions that get added to the list to be run. Code: $.onload(function(){if($.Class('Listener')){for(i=0;i<$.Class('Listener').length;i++){$.Class('Listener')[i].onclick=function(){alert($.getInfo(this).innerHTML);};}}},function(){if($.Class('Add')){for(i=0;i<$.Class('Add').length;i++){$.Class('Add')[i].innerHTML=$.Class('Add')[i].innerHTML+'...';}}}); Hi, I am trying to give the user the option of music on my homepage (b/c we've all found it annoying at times...perhaps most times). I have found an "onload" option for the body tag on dotnetspider.com, but it doesn't seem to be working for me. Please take a look and let me know of any improvements I could make. Thx Code: <body onload="loadMusic()"> <p> <script type="text/javascript"><!-- function loadMusic() { var r=confirm("Would you like a background tune?"); if (r==true) { <input type="button" onclick="show_confirm()" value="Show a confirm box" /> <object class="plugin_video" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="480" width="640"> <param name="autoplay" value="false" /><embed title="QuickTime" hidden="true" src="sounds/siteBG.wav" mce_src="sounds/siteBG.wav" autoplay="true" loop="true" controller="true"></embed> </object> } else { alert("If you change your mind reload the page to choose again."); // --></script> |