• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 417
  • Last Modified:

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
0
jpleaner
Asked:
jpleaner
  • 4
  • 3
  • 2
1 Solution
 
EarshelCommented:
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

0
 
jpleanerAuthor Commented:
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.
0
 
EarshelCommented:
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
sunithnairCommented:
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

0
 
jpleanerAuthor Commented:
As I have said a couple of times above, the javascript file containing the RemoveItemfromCart() function is encrypted.  I can not change it.
0
 
sunithnairCommented:
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.
0
 
sunithnairCommented:
Hello mate I think i have finally figured out how you can do this. Try this please
<html>
<head>
<script language='javascript'>
function RemoveItemfromCart(msg)
{
	alert("Hello " + msg);
}
</script>
</head>
<body>
<input type="image" src="Graphics/Close.png" onclick="javascript:RemoveItemfromCart('test');" style="border-width:0px;" />
 
</body>
<script language='javascript'>
    if (typeof(RemoveItemfromCart) == "function")
    {
        var oldRemoveItemfromCart = RemoveItemfromCart;
        RemoveItemfromCart = function(msg)
       {
	   //setTimeout(window.location.reload(true), 5000);
	   setTimeout("alert('modified hello');", 5000);
           oldRemoveItemfromCart(msg);
        }
    }
</script> 
</html>

Open in new window

0
 
jpleanerAuthor Commented:
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?
0
 
sunithnairCommented:
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;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now