Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>jQuery isn't causing a problem here. The issue is that the <code>href</code> property of HTMLAnchorElement (the type of object jQuery is returning), <a href="http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-48250443" rel="nofollow">per the spec</a>, always contains an absolute URI.</p> <p>In HTML5 <code>href</code> is <a href="http://dev.w3.org/html5/spec/urls.html#url-decomposition-idl-attributes" rel="nofollow">a composite attribute</a> and you can just swap the protocol (the part before <code>//</code>) at will by modifying <code>href.protocol</code>, e.g.:</p> <pre><code>var link = $( '&lt;a href="https://example.com/foo"&gt;bar&lt;/a&gt;' )[0]; console.log( link.href ); // =&gt; https://example.com/foo link.href.protocol = 'http:'; console.log( link.href ); // =&gt; http://example.com/foo </code></pre> <p>For older browsers without the composite <code>href</code> you'll just have to make do with a regex:</p> <pre><code>console.log( link.href ); // =&gt; https://example.com/foo link.href = link.href.replace( /^https:/, 'http:' ); console.log( link.href ); // =&gt; http://example.com/foo </code></pre> <p><strong>TLDR:</strong> Your code should look something like this:</p> <pre><code>$( "a[href^='/']" ).prop( "href", function( _idx, oldHref ) { return oldHref.replace( /^https:/, 'http:' ); } ); </code></pre> <p>P.S. You'll notice that I elided your <code>$.each</code> call. That's because <code>prop</code> automatically acts on every element in the matched set, i.e. it already does what you were doing with <code>each</code>.</p> <hr> <blockquote> <h3><a href="http://api.jquery.com/prop/#prop2" rel="nofollow">.prop( propertyName, function(index, oldPropertyValue) )</a></h3> <ul> <li><strong><code>propertyName</code></strong> The name of the property to set.</li> <li><strong><code>function(index, oldPropertyValue)</code></strong> 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 <code>this</code> refers to the current element.</li> </ul> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload