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 6543375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:20:22+00:00 2026-05-25T11:20:22+00:00

I am wanting to use jQuery to add the current browser window dimensions to

  • 0

I am wanting to use jQuery to add the current browser window dimensions to the end of the href of certain a tags.

The specific a tags all have their href begin with secondary_imgs.php but other than that, differ by containing data, i.e. secondary_imgs.php?Id=x&Title=y. I could easily set all these tags to have a specific class if that helps the problem.

I think the best approach to this would be to append the &width=x&height=y to the end of the href on document ready, and then set a second function to replace those values, whenever the page is resized.

Alas, I am at a loss as to how to complete this. I have the constituent parts (I think), but need help combining them in the proper way.

Here’s what I have thus far:

$(document).ready(function () {
    var width = $(window).width();
    var height = $(window).height();
    $("a[href^='secondary_imgs.php']").attr('href', function() {
        return this.append('&w=width&h=height') 
    });
});

I tried to specify just the a tags who’s href begins with secondary_imgs.php. I think that would be the best way of specifying it. But beyond that, I don’t know where I’m making a mistake.

For the second half of this, where the values are replaced when the window is resized, I thought using .resize() would be the best to trigger the function, however I don’t know how to set it up to replace the values, not add to them.

Any help would be much appreciated. Thank you.

  • 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-25T11:20:22+00:00Added an answer on May 25, 2026 at 11:20 am

    In the loop this refers to a DOM element, so you can’t use the append method on it. Besides, the append method adds elements inside an element, it doesn’t concatenate strings.

    To put the values of the width and height variables in the string you have to concatentate them into a string. Putting the variable names inside a string won’t evaluate them.

    $(document).ready(function () {
      var width = $(window).width();
      var height = $(window).height();
      $("a[href^='secondary_imgs.php']").attr('href', function(index, attr) {
        return attr + '&w=' + width + '&h=' + height; 
      });
    });
    

    If you want to get the window size at the moment that the user clicks on the link instead of the size when the page loads, then you would rather catch the click event, create a new URL and apply it yourself instead:

    $(document).ready(function () {
      $("a[href^='secondary_imgs.php']").click(function(e){
        e.preventDefault(); // stop the click
        var width = $(window).width();
        var height = $(window).height();
        var url = $(this).attr('href') + '&w=' + width + '&h=' + height;
        window.location.href = url; // replace the link action
      });
    });
    

    Edit:

    To be compatible with other scripts that take over the link action, you would make sure that you bind your event before loading/activating that script, and only change the URL and write it back, but first checking if there are properties that needs to be removed:

    $(document).ready(function () {
      $("a[href^='secondary_imgs.php']").click(function(e){
        var width = $(window).width();
        var height = $(window).height();
        var url = $(this).attr('href');
        var idx = url.indexOf('&w=');
        if (idx != -1) {
          url = url.substr(0, idx);
        }
        url += '&w=' + width + '&h=' + height;
        $(this).attr('href', url);
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wanting to use jQuery to wrap a mailto: anchor around an email address,
I am wanting to use jquery to do a show/hide of a DIV from
I'm wanting to use the jquery dialog to open a modal dialog, and in
I am wanting to use the Facebox plugin for JQuery but am having a
I have found a problem with use JQuery timeout, i have a page with
I am wanting to pass a variable to a jquery dialog window. I am
I'm wanting to use a bunch of client-side EJS (or jQuery) HTML Templates in
I'm wanting to use google maps and see a million ways to do it
For my blog I am wanting to use the Output Cache to save a
I'm looking at a new project and we are wanting to use Flex (to

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.