How do I access an ebay item's id via javascript within the listing?

595 Views Asked by At

We have a small javascript snippet which runs as part of our ebay item descriptions. The snippet requires the ebay item id.

In testing I found that there is a variable, ebayItemId, which seems suitable. However, if a user navigates directly to the item page (e.g. direct link) without navigating through other ebay pages first then the variable is not present.

How do I access the item id, or should I look to reading it from the DOM?


To elaborate a bit on the ebayItemId variable. I tested by opening a new private browser and navigating directly to the item's page. Interestingly it works as long as the item's page isn't the very first ebay page to be loaded--even with a forced full reload (Ctrl-F5).

2

There are 2 best solutions below

0
Luis Gallego On

Are you using the classic Ebay API? if so, I think you should check the documentation, there are lots of interesting functions you can use:

http://developer.ebay.com/DevZone/finding/CallRef/index.html

0
Hartmut On

Main reason it's failing in the case opening in a new browser is ebay's strange behaviour to render the item description of the first item page you visit not via iframe but directly uncached within their own DOM.

In this particular case those javascript variables are not preset so you can't access them.

A tiny workaround would be to read the item number / item ID directly from the item page itself (by using plain javascript). That's not a neat solution but should be working.

You can do e.g. like this to cover both scenarios:

var resolvedItemID = 
    typeof ebayItemID !== 'undefined' ? 
    ebayItemID : document.getElementById('descItemNumber') ? 
    document.getElementById('descItemNumber').innerText : null;

if(resolvedItemID) {
    ... your code ...
}