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 12 of 12
  1. #1
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post

    Script edit on other page

    Is there a way to make a script that edits something on a different page.

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    The question is a bit unclear but

    Yes, if you've opened that page in a window using javascript - then the script on the page in the opener window can do so (and vice versa).

    You can also use bookmarklets to apply javascript to any page...

  • #3
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Does your scenario involve
    - one user looking at two pages simultaneously,
    - or do you want a change made on one page to overwrite the values on another page that everyone will view (i.e. a page dynamically generated from database values),
    - or do you want the next page that the user visits on your site to depend on something they did on the first page?

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post
    I would wont if you bought an item then open a other page it would edit what you have.
    Last edited by sherlockturtle; 10-08-2011 at 09:53 PM.

  • #5
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    do you have a particular scenario in mind? I would like to recommend the third option to you for most cases, but if your scenario is different than my assumptions, then perhaps the second would be best.

  • #6
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    like a shopping cart, yeah, the third is what you need, but you might still need database access.

  • #7
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    You should really follow the forums guidelines about editing your previous posts.

    Because of your edit in which you changed your requirements to a scenario:

    1) I almost didn't read your comment giving a scenario because I thought I already read that post
    2) Someone might try to keep going with ironboys train of thought and give you more examples on my first suggestion even though you have already ruled that out because your post no longer shows that you've ruled it out
    3) The posts themselves make a little less sense with me asking you for a scenario after you have seemingly already provided one...

    PS If you absolutely must make a change that completley alters the original post you should document it somehow, and add to it, not rewrite it.

    Edit: I added the p.s.

  • #8
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    I can see that you have already researched cookies in another thread. So I would like to assume that you are not trying to alter the js on your new page, but rather modify it based on the cookies from the last page:
    Code:
    <div id="cart"></div>
    <script>
    myShoppingCart = GetMyCookies('shoppingcart')  // This assumes you have already built a cookie function and just need to extract the shopping cart variable
    
    myShoppingCart = myShoppingCart.split(',') // if you didn't already store the values in JSON format you  could just split them by a delimeter
    
    buildCart=''
    
    for(var i in myShoppingCart){
     buildCart +='<div id="item'+i+'">'+myShoppingCart[i]+'</div>'; 
     }
    document.getElementById('cart').innerHTML = buildCart;
    
    </script>
    Now of course I make a lot of assumptions in this example, but I don't know what you already have, so this is more a proof of concept to help you see how to do the following:

    1) Convert the values that you stored in your cookie (in what I assume to be a basic format with each item separated by a comma) into a useful array
    2) Build html code by looping through an array object (this can be used no matter how you configured your cookies as long as they are in an ordered array before you invoke this)
    3) Insert the pre-built html code into an already existing DIV element

  • #9
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post
    For the cookie i have been researching them but could you maby make a basic one to go with your example blaze?

  • #10
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Of course. But I don't have a cut and paste cookie function, so I'll just use the one recommended by jassi.singh in your cookie thread. Also, in a shopping cart you would typically use a form, and forms don't require cookies because you can actually just submit the form data directly to the server...

    Cookie.js (unedited from http://techpatterns.com/downloads/ja...pt_cookies.php )
    Code:
    function Set_Cookie( name, value, expires, path, domain, secure )
    {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );
    
    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if ( expires )
    {
    expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );
    
    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
    }
    // this fixes an issue with the old method, ambiguous values
    // with this test document.cookie.indexOf( name + "=" );
    function Get_Cookie( check_name ) {
    	// first we'll split this cookie up into name/value pairs
    	// note: document.cookie only returns name=value, not the other components
    	var a_all_cookies = document.cookie.split( ';' );
    	var a_temp_cookie = '';
    	var cookie_name = '';
    	var cookie_value = '';
    	var b_cookie_found = false; // set boolean t/f default f
    
    	for ( i = 0; i < a_all_cookies.length; i++ )
    	{
    		// now we'll split apart each name=value pair
    		a_temp_cookie = a_all_cookies[i].split( '=' );
    
    
    		// and trim left/right whitespace while we're at it
    		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
    
    		// if the extracted name matches passed check_name
    		if ( cookie_name == check_name )
    		{
    			b_cookie_found = true;
    			// we need to handle case where cookie has no value but exists (no = sign, that is):
    			if ( a_temp_cookie.length > 1 )
    			{
    				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
    			}
    			// note that in cases where cookie is initialized but no value, null is returned
    			return cookie_value;
    			break;
    		}
    		a_temp_cookie = null;
    		cookie_name = '';
    	}
    	if ( !b_cookie_found )
    	{
    		return null;
    	}
    }
    index.html
    Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    <style type="text/css">
    
    </style>
    
    <script defer type="text/javascript" src="lib/cookies.js"></script>
    
    
    <script type="text/javascript">
    
    cookieCart = [0,0,0];
    
     onload = function(){
      document.myForm.cookieOption1.onclick = function(){
       if(document.myForm.cookieOtion1.checked)
        cookieCart[0] = 'Chocolate Chip';
       else
        cookieCart[0] = 0;
       }
      document.myForm.cookieOption2.onclick = function(){
       if(document.myForm.cookieOtion2.checked)
        cookieCart[1] = 'Oatmeal';
       else
        cookieCart[1] = 0;
       }
      document.myForm.cookieOption3.onclick = function(){
       if(document.myForm.cookieOtion3.checked) 
        cookieCart[2] = 'Yummy';
       else
        cookieCart[2] = 0;
       }
      }
    
     function MAKE_COOKIES_COOKIE(){
      Set_Cookie('cookies',cookieCart.join(','));
      }
    
    </script>
    
    </head>
    
    <body>
     <form id="myForm" name="myForm" onsubmit="MAKE_COOKIES_COOKIE();" action="https://www.mysite.com/mycart.html" method="post">
      <input type="checkbox" name="cookieOption1">Buy My Chocolate Chip Cookies!!
      <input type="checkbox" name="cookieOption2">Buy My Oatmeal Cookies!!
      <input type="checkbox" name="cookieOption3">Buy My Yummy Chip Cookies!!
      <input type="submit" value="To Checkout">
     </form>
    </body>
    
    </html>
    mycart.html
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    <style type="text/css">
    
    </style>
    
    <script defer type="text/javascript" src="lib/cookies.js"></script>
    
    
    <script type="text/javascript">
    
     myShoppingCart = Get_Cookie('cookies')  
    
     myShoppingCart = myShoppingCart.split(',')
    
     buildCart=''
    
     for(var i in myShoppingCart){
      if(myShoppingCart[i])
       buildCart +='<div id="item'+i+'">'+myShoppingCart[i]+'</div>'; 
      }
    
    </script>
    
    </head>
    
    <body>
     <div id="cart"></div>
    </body>
    
    <script>
     document.getElementById('cart').innerHTML = buildCart;
    </script>
    
    </html>
    Again, I kept this very simple, and didn't use any techniques that would keep the code clean because it might make it difficult to understand the flow of logic if there were loops and such. So you will want to optimize it a little(ok, a lot!)...
    Now I want to stress that using cookies to make a shopping cart seems like a really bad idea to me. If you really want to make one but don't have the know how to use a serverside scripting language you should just look for a prebuilt free shopping cart with paypal already integrated...
    So there it is. I think I got everything, but to be honest, I'm a little drunk, and it's 4 in the morining, so I'm not going to test it (or even proof read it). I think I'm just gonna go sleep for like 16 hours. good luck


    Edit
    I forgot to test the cookie value before writing to the document- see blue changes
    Last edited by blaze4218; 10-09-2011 at 08:46 PM. Reason: I forgot to test the cookie value before writing to the document

  • Users who have thanked blaze4218 for this post:

    sherlockturtle (10-09-2011)

  • #11
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post
    thanks blaze. If you used submit how would you make the submit function flow data to the data base?

  • #12
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    That's an excellent question. You are going to need a server-side scripting language to write to the database. In order for anyone to help you on that, your going to need to first choose a server-side language. And questions regarding server-side scripting should go in the server side development forums in the appropriate language category. You could post them here, but you'll get better responses and feedback there.


  •  

    Posting Permissions

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