dumbnoob Posted December 28, 2009 Share Posted December 28, 2009 Ok, the following code, seems to work fine as it is, however, I need to be able to run it from the adress bar, and have it function the same as it does in a html page (with the exception of the onclick() call). <html> <head> <script type="text/javascript"> prerun=['1','5','3','2','6','12','15','16','7','11']; IT; function getCookie(c_name) { if (document.cookie.length>0) { c_start=document.cookie.indexOf(c_name + "="); if (c_start!=-1) { c_start=c_start + c_name.length+1 ; c_end=document.cookie.indexOf(";",c_start); if (c_end==-1) c_end=document.cookie.length return unescape(document.cookie.substring(c_start,c_end)); } } return "" } function setCookie(c_name,value,expiredays) { var exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays); document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString()); } function checkCookie() { thevar=getCookie('stupidcookie'); if (thevar!=null && thevar!="" && prerun[--thevar]!=undefined) { thevar = (++thevar); IT=(prerun[--thevar]); thevar = (++thevar); thevar = (++thevar); setCookie('stupidcookie',thevar,365); alert (IT); } else { thevar='1'; setCookie('stupidcookie',thevar,365); checkCookie() { } } } </script> </head> <body onLoad="checkCookie()"> </body> </html> So, in short, I need to be able to run it from the adress bar as Javascript:, and have it still work. I'm not exactly experienced in javascript, however, this will be an integral part of a multipart script I have written (That so far works perfectly) Any help would be appreciated, as I'm sure the answer is completely obvious. Quote Link to comment Share on other sites More sharing options...
.josh Posted December 29, 2009 Share Posted December 29, 2009 2 ways you can do this: 1) put everything completely in the address bar. You would basically need to wrap your code in the following: javascript:(function(){<code here>})() You may also need to urlencode your code (for instance, your spaces at least need to be changed to %20, other special chars may or may not work, depending on the browser) This would be okay for really short pieces of javascript. 2) do the same thing as #1, except the js to be put in the url would build and append a script tag with a src of your real javascript. This would obviously mean your real js would have to be hosted somewhere. But basically it would look something like this: javascript:(function(){var%20x=document.createElement(%22script%22);x.src='http://www.somesite.com/file.js';document.body.appendChild(x);})() you may need to add in other attribs to be cross-browser compatible, (like the script tag type), but you should be able to figure that stuff on your own. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.