...

View Full Version : how to set cookies on browser through javascript



esolve
10-08-2012, 05:56 PM
when I surf http://www.youtube.com/html5/?gl=FR&hl=fr
and click "join youtube html5"
I will notice that the visit creates a cookie on my firefox browser

the information is as follows, where "f1=40000000&f2=40000000" is the key

I want to create a blank html page which has javascript codes
when I visit this html page, I hope it will set the same cookies for me
I mean the domain is "youtube.com" and the content is f1=40000000&f2=40000000
expiry time is not important, as long as it can be maintained before the browser is closed
the reason for me to do this is at the end if you want to know



Host: .youtube.com
Name: PREF
Path: /
Content: f1=40000000&f2=40000000
Content raw: f1=40000000&f2=40000000
Expires: At end of session
Expires raw: 0
Send for: Encrypted connections only
Send for raw: true
Created: Mon 08 Oct 2012 01:10:42 PM UTC
Created raw: 1349701841568107
Last accessed: Mon 08 Oct 2012 01:10:46 PM UTC
Last accessed raw: 1349701846292406
HTTP only: No
HTTP only raw: false
This domain only: Yes
This domain only raw: true
Policy: no information available
Policy raw: 0
Status: no information available
Status raw: 0


I make a html file called test.html, and then I use firefox to open it, but I can't find any cookies related to my script on firefox, what is wrong with my html and javascript code?



<html>
<head>
<title>
JavaScript Example 1 -- Hello World
</title>
<script type="text/javascript">
<!--
function writeCookie()
{
var content="f1=40000000&f2=40000000";
var domain = "youtube.com";
var name = "PREF";
var path = "/";
var the_cookie = "name="+name+";content="+content+";domain="+domain+";path="+path+";expires=0;";
document.cookie = the_cookie;
}
writeCookie();
alert(document.cookie);
//-->
</script>
</head>
<body>
</body>
</html>



reason for doing this: the modified firefox will delete all cookies when it is closed, and I want to do some repeated test by scripts, so I want to use script to make this firefox to visit the created blank html page to get the cookie

Philip M
10-08-2012, 07:05 PM
If I understand you correctly, it is not possible because of the Same Origin Policy. Simply stated, the SOP states that JavaScript code running on a web page may not interact with any resource not originating from the same web site. That means that one website may not set or retrieve a cookie properly belonging to another site.


It is your responsibility to die() if necessary….. - PHP Manual

esolve
10-08-2012, 08:24 PM
yes, I noticed this later
so are there any other solutions?
I just want to enable html5 in youtube
http://www.youtube.com/html5

thanks


If I understand you correctly, it is not possible because of the Same Origin Policy. Simply stated, the SOP states that JavaScript code running on a web page may not interact with any resource not originating from the same web site. That means that one website may not set or retrieve a cookie properly belonging to another site.


It is your responsibility to die() if necessary….. - PHP Manual



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum