Link to home
Start Free TrialLog in
Avatar of jpleaner
jpleaner

asked on

Running a refresh script after another javascript has run

Hi Folks,

I hope that I can ask this question without going into the long story so here goes.  I am writing a website with a shopping cart in it.  I have found a shopping cart script which in its original form showed the shopping cart contents on the same page as the products.  I butchered this up a bit because I want the cart contents shown in a frame on the right side.

All works nicely except that when I press a "delete item" button which runs a script called RemoveItemfromCart(x) where x it the item number, it seems to require a refresh of that frame.  

Is there some way of watching for when the RemoveItemfromCart(x) script has run and then calling a refresh script.  Please note that I know the obvious answer is to do a refresh at the end of the RemoveItemfromCart(x) function, but the javascript file containing that function is encrypted.

I hope that I have explained myself well enough!

Thanks
Avatar of Earshel
Earshel

The first thing that came to mind was creating a function that first called the RemoveItemfromCart(x) function, then called the reload function after a short time (in this case, it's 500ms). The new function would be called when the button is pressed instead of the original function.
function RemoveItemAndRefresh(){
   RemoveItemfromCart(x);
   setTimeout(location.reload(true), 500);
}

Open in new window

Avatar of jpleaner

ASKER

The trouble with that is that the call to RemoveItemfromCart(x); is made when you press the delete item button on the shopping cart contects screen.  This screen is 'dynamically" created as you add items.  So i can't just jump into Dreamweaver and change the hyperlink on the button.  When you open the shopping cart screen in dreamweaver, the button doesn't exist yet.  Also, the javascript with the actual RemoveItemfromCart(x); code is encrypted.

Hence I was thinking of having something in my own code what "watched" for the RemoveItemfromCart(x);  to run and then called the refresh.

Please find attached the shopping cart contects page and the associated javascript file.
If it's dynamically created, do you have access to the code that creates it? If so, you can modify it so when the button is created, it's created to call the new function.
If you can change the function RemoveItemfromCart in the JS file then you can go it this way. This will reload the page in 5 seconds
function RemoveItemfromCart(x)
{
//existing code in the function
setTimeout(window.location.reload(true), 5000);
}

Open in new window

As I have said a couple of times above, the javascript file containing the RemoveItemfromCart() function is encrypted.  I can not change it.
I believe there is no function in javascript to monitor if a function is called or not but there is a function to watch if a variable has changed or not but this is not cross browser compatible https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/watch. If you cant change the javascript then try changing the serverside script as Earshel pointed out.
ASKER CERTIFIED SOLUTION
Avatar of sunithnair
sunithnair

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hey sunithnair,
Getting really close!  The code you write correctly identifies the RemoveItemfromCart function and then invokes the window.location.reload(true) function.  However, for some reason the refresh created by window.location.reload(true) is not the same as the refresh you get from ctrl+R in IE.  Is there a difference between location.reload and refresh button on the browser?
Sorry for the late reply. Have you solved the problem now? You could try any of the following
window.location.reload();
window.location.href=window.location.href;