SHARE
Facebook X Pinterest WhatsApp

HTML Goodies: Script Tip: Week 45

Written By
thumbnail Joe Burns
Joe Burns
Jan 4, 2005

 

Tip to my Lou…

     I received this “Proportional” script from Ann Evans. It’s a great idea. You enter the height and width of an image you want to resize. Then, by entering a new height, the script figures the new width, keeping the same proportions the image originally had.

     I really liked the script, but thought it asked too much of the user. I knew you could grab the image’s height and width straight from the image. Then, after setting the new height and width, it seemed logical to show the image in its new dimensions.

     So, using Ann’s original calculations and table structure, I rewrote and added on until I got this. Try it out.


When the prompt appears, put in “angel.jpg”
(without the quotes!)

See it in Action


    Did you like the purple background? I thought it was a bit easier on the eyes than bright white.

     The script uses a series of steps to complete the effect. We’ll start with the first thing you see on the page, the prompt. The blip of prompt code asks for the path to the image. Yes, I know it’s a little wordy and long to read.

     Once the prompt has the path and/or name of the image, then it uses that to create an image flag that posts the image to the page. Let’s look at it. Please understand that I have greatly reduced the amount of text in the prompt box for this example.

<SCRIPT LANGUAGE=”javascript”>

var path = prompt(“Text on the gray”,”Text in the box”)

if(path == “Text in the box”)
{
alert(‘Come on, put in an image name’)
javascript:location.reload()
}

if(path == null)
{
alert(‘Do not click Cancel’)
javascript:location.reload()
}

else
{document.write(“<IMG NAME=thepic SRC=” +path+ “>”);}

</SCRIPT>

     As I pointed out in the last series of Script Tips, one of the hardest things to do when writing a script is to guess at all the possibilities that could occur when someone uses your script. When this prompt popped up, I saw four events that could happen. Here are those events and my solutions:

  • The user enters a correct image name and/or path
         Solution: the image is posted
  • The user clicks OK, making the changes
         Solution: Alert box telling user to put in image name
  • The user clicks CANCEL
         Solution: Alert box asking for image name
  • The user enters incorrect information
         Solution: I can’t predict incorrect info, so that results in a broken image, which gives a 40X40 height and width

     I wrote the prompt above to cover each of those concerns. If you’d like to go back to the example and try each event, click here. Otherwise, let’s take it in order.

var path = prompt(“Text on the gray”,”Text in the box”)

     This is the basic format of a prompt. Again, I have greatly reduced the text used in the original, but you’ll get the idea. Whatever the user writes in the box is assigned the variable name “path.”

if(path == “Text in the box”)
{
alert(‘Come on, put in an image name’)
javascript:location.reload()
}

     The first If statement is used if the user simply presses OK without changing the information in the box.

    If the “path” equals “Text in the box” (note the double quotes so that ‘Text in the Box’ is seen as a text string rather than code), then pop up an alert that reads “Come on, put in an image name.” Once the user clicks OK on the alert box, javascript:location.reload()reloads the page and the prompt pops up again.

    Two things: There are curly brackets around the alert and the reload. That’s important so that the script understands to do both if the statement is true. Also, note that the text string is the same as what is in the text box on the prompt. Make sure those two are the same when you use the script or no dice.

if(path == null)
{
alert(‘Do not click Cancel’)
javascript:location.reload()
}

     Here’s the second If statement. The script goes to this one if the first isn’t found to be true. This is triggered if the prompt data is null. That means the user simply clicked CANCEL. Notice “null” is not in quotes. It is not a text string, but rather a command.

     The same basic format is used if this If statement is true. An alert pops up, the page is reloaded, and the prompt pops up again.

else
{document.write(“<IMG NAME=thepic SRC=” +path+ “>”);}

     If both of the If statements are false, which is what you’re hoping for, then a document.write statement is used to create a basic image flag, posting the picture the user has entered.

     See the NAME=thepicin the flag? That will become important in the next section of this Script Tip.

Next Week: Grab and Display the Dimensions

     Do YOU have a Script Tip you’d like to share? How about suggesting a Script Tip to write about? I’d love to hear it. Write me at: webmaster@htmlgoodies.com.


Learn to write your own JavaScripts with the

Recommended for you...

The Revolutionary ES6 Rest and Spread Operators
Rob Gravelle
Aug 23, 2022
Ahead of Time (AOT) Compilation in Angular
Tariq Siddiqui
Aug 16, 2022
Converting a JavaScript Object to a String
Rob Gravelle
Aug 14, 2022
Understanding Primitive Type Coercion in JavaScript
Rob Gravelle
Jul 28, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.