1

I'm trying to refresh an affiliate URL which is inside a piece of JavaScript with AJAX, but I can't get it to work.

Here's the code:

<script type="text/javascript">
    (function() {
        var mdWidgetUrl = "https://herecomestheurl";
        var s = document.createElement("script"), 
            s1 = document.getElementsByTagName("script")[0];
        s.type = "text/javascript";
        s.async = true;
        s.src = mdWidgetUrl;
        s1.parentNode.insertBefore(s, s1);
    })();
    function fetchdata(){
        $.ajax({
            url: 's.src',
            type: 'post',
            success: function(data){
                // Perform operation on return value
                alert(data);
            },
            complete:function(data){
                setTimeout(fetchdata,10000);
            }
        });
    }        
    $(document).ready(function(){
        setTimeout(fetchdata,10000);
    });                
</script>

What I'm trying to do is to AJAX reload the URL, which is known as "mdWidgetUrl", every 10 seconds without refreshing the whole page. This code doesn't work, because I don't know how to tag the s.src within the AJAX function.

Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
  • If you're going to edit and just remove all the content from the question then you might just delete it. This is not a valid edit – Alon Eitan Jan 10 '19 at 20:16
  • Thanks for your comment. I tried to delete it, but it seems that you can't delete a post when someone already posted a reaction. – ZnortDesigns Jan 11 '19 at 21:36

1 Answers1

0

Let's have a look to the different settings of your AJAX call:

url:

When you try:

url: 's.src',

you are not passing the content of the src property of the s object; you are literally passing the string "s.src". But even if you do:

url: s.src,

it won't work because s is out of scope. s was declared inside an IIFE ( (function() {... ) and it lives just inside it. You can not access it from outside.

Instead, after you create your s script, you can give it an id. Like this:

var s = document.createElement("script");
s.id = "mdWidgetScript";

Then, you can easily retrieve the value of the src attribute from the ajax call:

$.ajax({
  url: $("#mdWidgetScript").attr('src'),

Note that using an id is not mandatory. You could find the script like you found s1, selecting the first element with a <script> tag:

url: document.getElementsByTagName("script")[0].getAttribute("src");
// or with jQuery
url: $("script:eq(0)").attr('src');

I just find using an id a cleaner and more bulletproof way.

type:

It is an alias for method. You are retrieving data from the server, not sending to. Switch POST to GET (or leave this setting out, since GET is the default). Read this question about the differences.

dataType:

You should set the dataType accordingly (check this question). If the URL points to a script, use dataType: "script",. Actually, you could use $.getScript() which is a shorthand method for this kind of AJAX call.


If after properly tuning your settings you still have troubles:

David
  • 6,695
  • 3
  • 29
  • 46
  • Thanks for your answer! Sadly enough I don't have access to the script where the s.src is coming from. Also I don't have much understanding of JS in general. So I think your answer is pretty clear, I can't implement it. Thanks for the effort anyways! – ZnortDesigns Oct 29 '18 at 21:59
  • @ZnortDesigns - There is just one script in your code so I guess you mean that you don't have access to the code inside the IIFE. As you can read at the end of the answer, you can get the `src` attribute **without** using an id. In that case no edition of the `s.src` is required. – David Oct 29 '18 at 22:01
  • @ZnortDesigns - In case the last bit was confusing: just replace `url: 's.src',` for `url: $("script:eq(0)").attr('src'),`. Maybe you need to change the `0` if other scripts are being loaded before in the same page. – David Oct 29 '18 at 22:08
  • I tried to implement it the way you told, it's now showing the product again, but it doesn't refresh after 10 seconds. Can I send you the URL, so that you can see how the code is looking now? – ZnortDesigns Oct 30 '18 at 19:53
  • @ZnortDesigns. sure, send the URL. Using `setInterval` is not the way to fix it. If the `complete` callback is not running is because you have a deeper problem with your AJAX call. Read the answers to [this question](https://stackoverflow.com/q/2787180/5247200). Also, I have updated my answer, so far I focus just on the `url:` bit. – David Oct 30 '18 at 20:48
  • @ZnortDesigns I would recommend adding, underneath the line where you set `s.async`: `s.id = "affiliateLinkSource";` Then, in the `$.ajax` section change `'s.src'` to `$("#affiliateLinkSource").attr('src')` – NBTX Oct 30 '18 at 20:57
  • 1
    @SamJakob - That's exactly what my answer is about. – David Oct 30 '18 at 21:00
  • 1
    @David I know, that's why I left a comment instead of a separate answer. I just figured using an ID was a cleaner / more reliable approach and I tried to clarify what you meant. – NBTX Oct 30 '18 at 21:01