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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:57:45+00:00 2026-05-27T16:57:45+00:00

I am looking for the proper method to embed javascript into an html parameter.

  • 0

I am looking for the proper method to embed javascript into an html parameter. This is an example of what I am trying to do:

  <img src="source.png" height="<script> if(screen.width == 1920)
                                {document.write('60px');}</script>" 
                                 title="icon"/>

I know embedding PHP in this manner can work, but I can not seem to get it to work with Javascript. My limited knowledge on the matter is to blame, and I need some help resolving this issue. 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-27T16:57:46+00:00Added an answer on May 27, 2026 at 4:57 pm

    Note the id="sourceImg" I added to the element, which must be unique per element per page. Not doing this will mean Javascript will not know which element to select, and will pick one.

    <img id="sourceImg" src="source.png" title="icon">
    
    <script>
    if (screen.width == 1920) {
        document.getElementById('sourceImg').style.height = '60px';
    }
    </script>
    

    That either needs to run after the markup falls in the source of the page, or in a window.onload event handler. If it runs before the img element is available, it will error out.

    NOTE – JohnFx’s method that he describes in the comments below is:

    <img id="sourceImg" src="source.png" title="icon" onload="sizeMe()">
    
    <script>
    function sizeMe() {
      if (screen.width == 1920) {
        document.getElementById('sourceImg').style.height = '60px';
      }
    }
    </script>
    

    This uses the img element’s onload handler. Note that inline event handlers like onload="", onclick="", etc., are typically considered hard to maintain. The above should work, however, as a demonstration, and other routes involve understanding page loading, DOM element availability and other details which are potentially more difficult to understand than a simple example.

    EDIT

    And, technically, you could do this:

    <img id="sourceImg"
         onload="if(screen.width < 1920) this.style.width='60px';"
         src="http://upload.wikimedia.org/wikipedia/commons/thumb/a/ae/001_Jornal_Monsenhor_A_Comarca_23_de_Marco_de_1947.jpg/762px-001_Jornal_Monsenhor_A_Comarca_23_de_Marco_de_1947.jpg"
         title="icon">
    

    http://jsfiddle.net/pdN3y/

    Note, I changed the if statement to make the change more obvious.

    But please don’t. It’s a bad idea and a bad habit to get into. Spend some time figuring out page loading and DOM availability, it’s time well spent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a good JavaScript RegEx to convert names to proper cases. For
I'm looking to match a part of several HTML files that get passed into
I am looking into the unittest package, and I'm not sure of the proper
I am looking for the most proper and efficient way to bind Javascript events
I'd like my website to have URLs looking like this: example.com/2010/02/my-first-post I have my
I have been looking at using TDD and implementing proper testing (only just started
Looking for an example that: Launches an EXE Waits for the EXE to finish.
I've been looking for the answer to this question for a while now but
I am currently looking for a proper way to restrain the usage of a
I'm looking for a good, solid reference for proper RDoc syntax. Recommendations? I can't

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.