Can anyone help me with this blogspot script?

by 7 replies
7
Before I was looking for a script which instantly redirects the visitor to another url.

I was provided with one at another forum, here it is here:

<script type='text/javascript'>
var d='<data:blog.url/>';
d=d.replace(/.*\/\/[^\/]*/, '');
location.href = 'http://insertyoursiteurlhere.com/';
</script>


I put it under the head part of the blogspot html, and it does work, but for some reason it automatically end up like this after I save it:

<script type='text/javascript'>
var d='<data:blog.url/>';
d=d.replace(/.*\/\/[^\/]*/, '');
location.href = 'http://insertyoursiteurlhere.com/';
</script>


It adds a ' to the end of the destination url, which directs the visitor to a 404 error page, and this is no good.

Is there anything I can do about this?
#programming #blogspot #script
  • For some reason this forum isn't allowing me to post the second code in here with the extra & # 39
  • Hey there, here's what you have...





    You are creating variable called 'd', and you're loading the url into it. Then, the d=d.replace(/.*\/\/[^\/]*/, ''); part does some fancy footwork to remove everything between the first appearance of two slashes '//' and the first appearance of a single slash '/' - this effectively turns 'http://test.com/blog-post-title' into just 'blog-post-title'.

    So, you do all that work with that d variable, but then you don't actually use it at all. From the looks of your code, you probably need to do something like...





    I made two changes. Both changes are to the link beginning with 'location.href'

    - I removed the trailing slash from the url (http://insertyoursiteurlhere.com/ to just http://insertyoursiteurlhere.com). The line before effectively takes http://oldurl.com/blog-post-title and leaves you with just /blog-post-title - see that it already has a slash, so there's no need to have the slash at the end of your url on this line.
    - I also added + d to the end of the line. That will make the script redirect a visitor from http://oldurl.com/blog-post-title to http://newurl.com/blog-post-title

    From what I can see, the code is solid. Try that out and see if it corrects your issue.
    • [ 1 ] Thanks
    • [1] reply
    • Good show, Noah!

      All I'd add is that it seems like the three backslashes (before each forward slash) may have gotten stripped out, (possibly by the WF posting mechanism?), so it may be necessary to re-insert them into the actual code.
      • [ 1 ] Thanks
      • [1] reply
  • Haha. It never ends. Got it fixed!
  • Have you tested this very simple redirect script? It will redirect the user to that specific page that you have defined in window.location. No fancy regex or other stuff. One line code only

    <script>
    window.location = "http://www.yoururl.com";
    </script>

    Remember this will redirect the user from that page where that script is located.

Next Topics on Trending Feed