Can anyone help me with this blogspot script?

7 replies
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?
#blogspot #script
  • Profile picture of the author karmadog
    For some reason this forum isn't allowing me to post the second code in here with the extra & # 39
    {{ DiscussionBoard.errors[10600843].message }}
  • Profile picture of the author noah.whitmore
    Hey there, here's what you have...


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



    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...


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



    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.
    Signature
    No Pitch For The Moment - Just A Nice Hello.
    So... 'Hello'
    Feel free to PM me if you have any questions about my posts. I'd like to hear from you!
    {{ DiscussionBoard.errors[10600997].message }}
    • Profile picture of the author David Beroff
      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.
      Signature
      Put MY voice on YOUR video: AwesomeAmericanAudio.com
      {{ DiscussionBoard.errors[10601111].message }}
      • Profile picture of the author noah.whitmore
        Originally Posted by David Beroff View Post

        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.
        Thank you for noticing that David! You are exactly correct. When using the special code formatting that the forum allows, it change

        d=d.replace(/.*\/\/[^\/]*/, '');
        to
        d=d.replace(/.*//[^/]*/, '');


        It removed all of my escape slashes. How annoying. I've updated my response above to correct this.

        -Noah
        Signature
        No Pitch For The Moment - Just A Nice Hello.
        So... 'Hello'
        Feel free to PM me if you have any questions about my posts. I'd like to hear from you!
        {{ DiscussionBoard.errors[10601377].message }}
  • Profile picture of the author noah.whitmore
    Haha. It never ends. Got it fixed!
    Signature
    No Pitch For The Moment - Just A Nice Hello.
    So... 'Hello'
    Feel free to PM me if you have any questions about my posts. I'd like to hear from you!
    {{ DiscussionBoard.errors[10601765].message }}
  • Profile picture of the author Lokki08
    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.
    {{ DiscussionBoard.errors[10601820].message }}

Trending Topics