infinite-ajax-scroll icon indicating copy to clipboard operation
infinite-ajax-scroll copied to clipboard

Exclude the 'javascript:;' from the next url

Open kzhang-dsg opened this issue 4 years ago • 5 comments

Description
Some of the pagination libraries put 'javascript:;' in the next URL when reached to the last page. Currently, the library throws an error:

Access to XMLHttpRequest at 'javascript:;?1597427851817' from origin 'https://xxxxxx' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

Can we validate the next URL, if it is not a valid URL, consider it as a last page?

kzhang-dsg avatar Aug 14 '20 18:08 kzhang-dsg

Some of the pagination libraries

Do you have any examples?

It sounds like something very specific and I'm not sure if I should bloat IAS to support every other lib out there, but if this is a common thing or used by large libs I can consider it.

fieg avatar Nov 30 '20 15:11 fieg

Hi fieg,

You can check this example out: https://us.brinks.com/insights. See the next button at the bottom of the page when you go to the last page. The href of the next button becomes href="javascript:;" on the last page.

We dont need to do library-specific logic. Just need to do an additional check to make sure the next button URL is valid before requesting it.

I know there is no customer support for the paid customer but I did pay for the license. I think it will help others to have this feature.

kzhang-dsg avatar Nov 30 '20 15:11 kzhang-dsg

Thank you for your reply!

I see you point and agree, which leads me to a few questions:

  • What is a valid url and what's not? And how can we validate that?
  • Should IAS do this check, or should it allow users to implement this check themselves, in the few cases where this is needed?
  • Should IAS perhaps execute javascript prefixed hrefs? Not sure if this is even possible tho.

If you have thoughts on these questions, please share.

fieg avatar Nov 30 '20 17:11 fieg

We can check if the URL is a valid URL by using regex: https://stackoverflow.com/questions/1303872/trying-to-validate-url-using-javascript, or simply check if the URL starts with https?://

To get the absolute URL, we can use this: https://stackoverflow.com/questions/3943281/resolving-relative-urls-in-javascript

It turns out that the .href attribute of a A element (not .getAttribute('href'), but .href) returns the resolved (absolute) URL.
  1. For <a href="https://example.com/page1">, the .href will return https://example.com/page1;
  2. For <a href="/page1">, the .href will return https://example.com/page1;
  3. For <a href="javascript:;">, the .href will return javascript:;;
  4. For <a href="javascript:alert('111');">, the .href will return javascript:alert('111');;

Once we got the absolute URL, we can check if the URL is valid or not.

kzhang-dsg avatar Nov 30 '20 17:11 kzhang-dsg

Thank you for your detailed reply 🙏 I will take it into consideration. If you are up for it, feel free to send in a PR with a code proposal.

fieg avatar Dec 07 '20 10:12 fieg