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

  • Home
  • SEARCH
  • 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 3979496
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:10:39+00:00 2026-05-20T05:10:39+00:00

javascript noob trying to figure something out. I saw a script located here: Using

  • 0

javascript noob trying to figure something out. I saw a script located here:

Using jQuery to open all external links in a new window

in which urls on a page are checked to see if they match the current page url. If not, this particular script would open them in a new window. I would like to use the same test on urls but if they are external I would like ?iframe to be appended to each url, a la fancybox syntax. My noob script is:

$(document).ready(function() {
    $('a[@href^="http://"]').filter(function() {
        return this.hostname && this.hostname !== location.hostname;
    }).append('?iframe')
});

which I believe is incorrect. Any assistance? Thanks

  • 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-20T05:10:39+00:00Added an answer on May 20, 2026 at 5:10 am

    You have to change the href attribute using attr, not append new child elements:

    $('a[href^="http://"]').filter(function() { // no @ before the attribute name
        return this.hostname && this.hostname !== location.hostname;
    }).attr('href', function(i, value) {
        // takes care of URLs with parameters
        return value + (value.indexOf('?') > 0 ? '&' : '?') + 'iframe';
    });
    

    Note that hostname is only available in HTML5. So in browsers not supporting HTML 5, the filter would discard every element (Update: Given that the question you linked to is from 2009, it seems browsers implemented hostname but it was not part of any specification). You can change it to:

    var hostname = new RegExp("^http://" + location.hostname);
    
    $('a[href^="http://"]').filter(function() {
        return !hostname.test(this.href);
    }).attr('href', function(i, value) {
        return value + (value.indexOf('?') > 0 ? '&' : '?') + 'iframe';
    });
    

    And if you also want to convert links to https websites, ditch the attribute selector and change the regular expression:

    var hostname = new RegExp("^https?://" + location.hostname);
    
    $('a').filter(function() {...
    

    Update:

    Specifically for fancybox, according to the documentation, it should work this way. However, you are right, you could just add the iframe class:

     $('a').filter(function() {...}).addClass('iframe');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am an extreme noob with jQuery. I'm trying to show an item based
My JavaScript is pretty nominal, so when I saw this construction, I was kind
I confess, I'm a javascript noob. Part one of my current task is I'm
I've been trying to get my head around JavaScript inheritance. Confusingly, there seem to
Greets! I'm a noob struggling to learn html and javascript - getting there slowly.
I'm trying to modify the code from this script . Basically I'm trying to
I'm a complete noob to jquery and jquery ui but have successfully implemented a
I'm trying to add consecutive classes to all list-items in a list with the
JavaScript (JQuery) $('input').keyup(function(e) { var code = e.keyCode ? e.keyCode : e.which; switch(code) {
javascript:var width=1900; var height=800; var jQueryScriptOutputted = false; function initJQuery() { if (typeof(jQuery) ==

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.