Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-08-2012, 05:56 PM   PM User | #1
esolve
New Coder

 
Join Date: Oct 2012
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
esolve is an unknown quantity at this point
how to set cookies on browser through javascript

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

Code:
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?

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

Last edited by esolve; 10-08-2012 at 07:02 PM..
esolve is offline   Reply With Quote
Old 10-08-2012, 07:05 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 10-08-2012, 08:24 PM   PM User | #3
esolve
New Coder

 
Join Date: Oct 2012
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
esolve is an unknown quantity at this point
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

Quote:
Originally Posted by Philip M View Post
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 is offline   Reply With Quote
Reply

Bookmarks

Tags
browser, cookie

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:38 PM.


Advertisement
Log in to turn off these ads.