Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 08:02 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    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.

  • #3
    New Coder
    Join Date
    Oct 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •