...

View Full Version : Script edit on other page



sherlockturtle
10-08-2011, 04:08 PM
Is there a way to make a script that edits something on a different page.

ironboy
10-08-2011, 07:08 PM
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...

blaze4218
10-08-2011, 09:27 PM
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?

sherlockturtle
10-08-2011, 09:47 PM
I would wont if you bought an item then open a other page it would edit what you have.

blaze4218
10-08-2011, 09:53 PM
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.

blaze4218
10-08-2011, 10:02 PM
like a shopping cart, yeah, the third is what you need, but you might still need database access.

blaze4218
10-08-2011, 10:21 PM
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.

blaze4218
10-08-2011, 10:41 PM
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:



<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

sherlockturtle
10-08-2011, 11:59 PM
For the cookie i have been researching them but could you maby make a basic one to go with your example blaze?

blaze4218
10-09-2011, 02:09 PM
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/javascript_cookies.php )


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


<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


<!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

sherlockturtle
10-09-2011, 03:31 PM
thanks blaze. If you used submit how would you make the submit function flow data to the data base?

blaze4218
10-09-2011, 08:54 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum