Click here to Skip to main content
15,885,754 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a download page on my WordPress blog. On this page, I want to grab an HTML element with id="download" from any URL that linked to the download page.

please how do I accomplish this? look at my code below, but it is not working

What I have tried:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>

<script type="text/javascript">
var pagereferrer = Document.referrer;
var timeleft = 10;
var downloadTimer = setInterval(function(){
timeleft--;
document.getElementById("countdowntimer").textContent = timeleft;
if(timeleft <= 0)
clearInterval(downloadTimer);
},1000);

$( "#div1" ).load(pagereferrer #download);
</script>



<h2>
<span style="font-family: 'comic sans ms', sans-serif;">
Please wait ! Your download will begin in <span id="countdowntimer">10 </span> Seconds.......
</span>
</h2>

<div id="div1"> </div>
Posted
Updated 11-Feb-20 2:57am
Comments
F-ES Sitecore 11-Feb-20 8:17am    
Unless I'm mistaking your question, you can't. If you have gone from pagea to pageb, then pagea is gone and everything on it no longer exists so you can't access anything that was on it.
Bill Mayheptad Ritchie 11-Feb-20 12:37pm    
if this your statement is true, how did w3school manage to make this work


$(document).ready(function(){
$("button").click(function(){
$("#div1").load("demo_test.txt #p1");
});
});


I think the problem is maybe jquery .load method does not accept variables as the URL parameter

As your question is described, what you want is not possible.

Document.referrer only gives you the URL of the page that got you to this page. The DOM for that page no longer exists, so the fields you're looking for values from also no longer exist.

The only way I know of to get the value from the previous page is if that page passes the values you're looking for to the server controller and that controller places the values into the new page being served up.
 
Share this answer
 
Comments
Bill Mayheptad Ritchie 11-Feb-20 12:41pm    
than you @dave kreskowiak
please if you know any other way to accomplish this task, please help me
Assuming the referrer is a page on the some origin, then the load method should work:
JavaScript
var pageReferrer = document.referrer;
if (pageReferrer) {
    $("#div1").load(pageReferrer + " #download");
}
.load() | jQuery API Documentation[^]

NB: Pay attention to the documentation:
Quote:
Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy[^]; the request can not successfully retrieve data from a different domain, subdomain, port, or protocol.

Also note that the referrer might not be set, or might not contain the full URL, depending on the referrer policy set for the referring page:
Referrer-Policy - HTTP | MDN[^]
 
Share this answer
 
Comments
Bill Mayheptad Ritchie 11-Feb-20 12:51pm    
this still doesn't work,
the page is on the same domain https://howtechwork.com

but if I may ask, how did the https://thewpclub.net and https://www.neesrom.com
manage to get all the download link on their blog to redirect to the same particular page and ten display the download link on that page after some seconds countdown
Richard Deeming 11-Feb-20 13:10pm    
"Doesn't work" doesn't give us much to go on. What happened when you debugged the code? What value was returned by the document.referrer property? What response did you get to the network request? What errors (if any) were logged in the console?
Bill Mayheptad Ritchie 11-Feb-20 13:49pm    
thanks for your response @richard deeming.
there is no error in the console.
and the countdown timer on the download page work just fine.
but still, the jquery .load method did not seem to load the element with id="download" from the referring page into the selected div

but after removing the #download from the .load method, I only provide the pageReferrer variable, it loads the content into the div but the page appears like its auto-reloading the content.

look at the source code on the page https://howtechwork.com/downloadfiles/
<h2><span style="font-family: "comic sans ms", sans-serif">Please wait ! Your download will begin in <span id="countdowntimer">10 </span> Seconds.......</span></h2>

<div id="div1"></div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script><script type="text/javascript">var pageReferrer = document.referrer;var timeleft = 10;var downloadTimer = setInterval(function(){timeleft--;document.getElementById("countdowntimer").textContent = timeleft;if(timeleft <= 0)clearInterval(downloadTimer);},1000);    $("#div1").load(pageReferrer);</script>
Richard Deeming 11-Feb-20 14:51pm    
Looking at the source of that page isn't going to help. You need to start with a page on your site that contains an element with the ID "download", which refers the user to your "downloadfiles" page.

As I said, you need to debug your script to see what the document.referrer property returns, and examine the network request to make sure the AJAX request returns the page you were expecting.
Bill Mayheptad Ritchie 12-Feb-20 5:42am    
thanks, @Richard Deeming, with the various debugging technique I have run so far.
I think the jquery .load function is loading the entire document of the referring page into the div. which is causing duplicate content in the console.

please if u no a better solution to my problem pls provide

all I was just trying to do is to send all the download links on each of my blog posts to howtechwork.com/downloadfiles then display the download link there.

just like thewpclub.net and https://www.neesrom.com did it on their blog.

please assist me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900