Jump to content

Run Cookie stuff from adress bar.


dumbnoob

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.