Javascript target after ajax call
I am a little confused and the problem can probably be resolved in a matter of seconds for someone in the know.
I have a product viewing page that has the ability to add to cart. Once the user clicks "add to cart" it executes an ajax script and runs an asp page in the background to add the product to the cart database table. once this happens I count the number of total products in the cart with a simple function and want to display it on the page automatically. I am giving the display job to Javascript like this:
<script la开发者_JAVA技巧nguage="javascript" type="text/javascript">
var cartnum = ' (<%=CountItemsInCart()%>)';
top.getElementById('NumberOfItemsInCart').innerHTML = cartnum;
</script>
This is all in the ajax ASP page
However the javascript is failing, saying, target is NULL. How do I make the change to the default document which has the ID "NumberOfItemsInCart"?
Any help you can provide would be greatly appreciated.
Many thanks,
Paul
If you're calling the page via AJAX, this has no reference to the page you have open currently. You would need to handle this in the AJAX call when it returns instead. You can return the Count from the ASP page, and then update the page using Javascript at that point. If you post that portion of your code, I'm sure someone could help you with the placement.
Look into using window.opener.document. Check here and see if that helps point you in the right direction.
Here's a quick snippet:
window.opener.document["nameForm"].getElementById("someid").value;
Like Shawn's answer, Ajax calls should only return some json/xml. You need to update your html from the callback.
精彩评论