Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7577743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:15:59+00:00 2026-05-30T17:15:59+00:00

Hopefully someone can explain some odd behavior I’ve encountered with jQuery. The following script

  • 0

Hopefully someone can explain some odd behavior I’ve encountered with jQuery. The following script is looking for relative links on my page and replacing them with absolute links.

$(document).ready(function() {
  $("a[href^='/']").each(function(){ 
    var cur_href = $(this).prop("href");
    $(this).prop("href", 'http://www.mysite.com'+cur_href);
  });
});

I’m using this script on a page that will be served up over https but I don’t want all of my navigation to link to https pages. Since my navigation are global includes, this seemed like the easiest way to fix the problem.

The issue I’m encountering comes in the actual replacement. The second line of the script correctly matches all relative links on the page and then runs the replacement part of the script. It is in the replacement, line 4, where I get some weird results. After this part of the script runs, my URLs end up looking like this:

http://www.mysite.comhttps//www.mysite.com/mypage.htm

Obviously isn’t doing what I want. It seems like the first part of the script is matching the relative URL but when the replacement part fires the browser has already tacked on the domain information.

The only thing I’ve found so far that actually does what I want is to write the replacement anticipating what the browser has tacked on:

this.href = this.href.replace(/^https:\/\/www\.mysite\.com\//, "http://www.mysite.com/");

Is there a better way to do this?


Edit: here is a jsfiddle of the problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:16:00+00:00Added an answer on May 30, 2026 at 5:16 pm

    jQuery isn’t causing a problem here. The issue is that the href property of HTMLAnchorElement (the type of object jQuery is returning), per the spec, always contains an absolute URI.

    In HTML5 href is a composite attribute and you can just swap the protocol (the part before //) at will by modifying href.protocol, e.g.:

    var link = $( '<a href="https://example.com/foo">bar</a>' )[0];
    console.log( link.href );
    // => https://example.com/foo
    
    link.href.protocol = 'http:';
    console.log( link.href );
    // => http://example.com/foo
    

    For older browsers without the composite href you’ll just have to make do with a regex:

    console.log( link.href );
    // => https://example.com/foo
    
    link.href = link.href.replace( /^https:/, 'http:' );
    console.log( link.href );
    // => http://example.com/foo
    

    TLDR: Your code should look something like this:

    $( "a[href^='/']" ).prop( "href",
      function( _idx, oldHref ) {
        return oldHref.replace( /^https:/, 'http:' );
      }
    );
    

    P.S. You’ll notice that I elided your $.each call. That’s because prop automatically acts on every element in the matched set, i.e. it already does what you were doing with each.


    .prop( propertyName, function(index, oldPropertyValue) )

    • propertyName The name of the property to set.
    • function(index, oldPropertyValue) A function returning the value to set. Receives the index position of the element in the set and the old property value as arguments. Within the function, the keyword this refers to the current element.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain my problem, and hopefully someone can offer some good advice. I
Can someone (hopefully Rob) explain how to implement the new SubSonicRepository<> Pattern? I have
Maybe I'm a little behind the eight ball. Hopefully someone can explain this. I
Hopefully someone can help me out. Been all over google now. I'm doing some
hopefully someone can help me with this odd problem. I have a database which
Hopefully someone can shed a little light on an issue that I'm currently having
I have a weird date rounding problem that hopefully someone can solve. My client
Slightly strange question, but hopefully someone can help. In essence, if the time was
This is a weird one, but hopefully someone can give me an idea here.
Ok, this is bit of an obscure question, but hopefully someone can help me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.