0

there is a script I'm trying to use for Blogger. It works when you enter your domain URL into the src, but I'm trying to find a way to do it using hostname to insert the domain.

Original script:

<script type="text/javascript" src="http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=INSERT-YOUR-URL-HERE&amp;ShowHowMany=5&amp;_id=390e906036f48772b2ed4b5d837af4cd&amp;_callback=getYpipePP&amp;_render=json" />

I tried:

<script type="text/javascript">
    var excuteTopCommentators = "http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'+window.location.hostname+'&amp;ShowHowMany=5&amp;_id=390e906036f48772b2ed4b5d837af4cd&amp;_callback=getYpipePP&amp;_render=json"
    return excuteTopCommentators
</script>

I also tried document.write:

<script type="text/javascript">
    document.write('<script type="text/javascript" src="http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'+window.location.hostname+'&amp;ShowHowMany=5&amp;_id=390e906036f48772b2ed4b5d837af4cd&amp;_callback=getYpipePP&amp;_render=json"></script>');
</script>

Neither of the attempts seem to work. Any ideas on how to do this without manually inserting the domain URL into the script src?

Xarcell
  • 2,011
  • 6
  • 33
  • 65

3 Answers3

0

Perhaps the problem is that you get the script, but it is never executed.

Maybe try jQuery's getScript http://api.jquery.com/jquery.getscript/

function getIt() {
  return $.getScript('http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'+window.location.hostname+'&amp;ShowHowMany=5&amp;_id=390e906036f48772b2ed4b5d837af4cd&amp;_callback=getYpipePP&amp;_render=json');
}
Elise Chant
  • 5,048
  • 3
  • 29
  • 36
0

Your second option looks fine:

<script type="text/javascript">
    document.write('<script type="text/javascript" src="http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'+window.location.hostname+'&amp;ShowHowMany=5&amp;_id=390e906036f48772b2ed4b5d837af4cd&amp;_callback=getYpipePP&amp;_render=json"></script>');
</script>

The script is downloaded but do nothing. this is because it is a JSONP, not an script

So, you need a callback function to get it working: (note that is data, not code)

<script type="text/javascript">
    // JSONP Callback
    function getYpipePP(data) {
        alert(JSON.stringify(data));
    }

    document.write('<script type="text/javascript" src="http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'+window.location.hostname+'&amp;ShowHowMany=5&amp;_id=390e906036f48772b2ed4b5d837af4cd&amp;_callback=getYpipePP&amp;_render=json"></script>');

</script>

http://plnkr.co/edit/HhYZtH09EcdPZbwS9rfq?p=preview

Look the query, say _callback=getYpipePP and _render=json

Related: What is JSONP all about?

Community
  • 1
  • 1
rnrneverdies
  • 15,243
  • 9
  • 65
  • 95
  • Didn't work: Uncaught SyntaxError: Unexpected identifier. I suspect it may have something to do with the fact that this is a XML template. I tried adding CDATA tags to your script, but then got the error illegal token. – Xarcell Nov 22 '14 at 04:28
  • @Xarcell its works, you are fetching a JSONP OBJECT not an script. see the plunker, and see the fetched data, just call a function called getYpipePP and pass an JSON object as parameter. Just that jsonp does. – rnrneverdies Nov 22 '14 at 04:30
  • Sorry, but it doesn't. The output is `');` and it throws the error in the console. – Xarcell Nov 22 '14 at 04:34
  • It's a shame you do not see the answer to your problem when you have it in front of your eyes. – rnrneverdies Nov 22 '14 at 04:59
0

Try this (the problem you're having is the end "script" tag):

<script type="text/javascript">
        document.write('<script language="javascript" src="http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'
                     + window.location.hostname
                     + '&amp;ShowHowMany=5&amp;_id=390e906036f48772b2ed4b5d837af4cd&amp;_callback=getYpipePP&amp;_render=json" type="text/javascript"><\/script>')
    </script>
osdreams
  • 9
  • 2