PDA

View Full Version : Use text input to determine URL to picture and load onto same page


postitlord
10-08-2004, 01:07 AM
I want a single text box, a single button, and when you type in, for example, 251-1606 it loads http://www.radioshack.ca/images/RadioShack/25/2511606l.jpg (http://www.radioshack.ca/images/RadioShack/25/2511606l.jpg). I'm aware of forms, and I'm comfortable with figuring out the JavaScript, but I have no idea how to put the two together to accomplish this.

Refinement #1. Instead of loading the image to fill the window as though it were clicked, I'd like it to appear in that big empty space below the text box and button. Since frames are collectively considered evil, can the image appear on the same page as the form? Target computers use Windows 2000, Internet Explorer 6.

Refinement #2. I don't know if this one's possible. For each different image fetched, instead of simply replacing the image with the newest one, I'd like them to start accumulating on the same page. The newest one at the top.

For those who are interested, I'm a sales associate at RadioShack, and have discovered the people who maintain radioshack.ca the do an excellent job of it. But often we'll have old stuff, and when we're holding the price tag for mystery item 5000, we'll go to the web site, type in a stock number like above, and appears exactly what were looking for. Except for old stuff. That's been removed from public searches. The only way to find discontinued product images is type the URL manually. Bleah.

coothead
10-08-2004, 06:41 PM
Hi there postitlord,

I've been thinking about this one on and off all day :D
Finally came up with this...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Display Picture</title>

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

<style type="text/css">
/*<![CDATA[*/
body {
margin-left:30px;
}
div {
margin:2px;
}
input {
width:100px;
text-align:center;
}
img {
border:solid 1px #000000;
display:none;
}
/*//]]>*/
</style>

<script type="text/javascript">
//<![CDATA[

var i=0;
var url="http://www.radioshack.ca/images/RadioShack/25/";

function displayPic() {

var pattern=document.forms[0][0].value;
var str = pattern.replace(/-/g,'');
document.getElementById("pic"+i).style.display="block";
document.getElementById("pic"+i).src=url+str+"l.jpg";
i++;
}

//]]>
</script>

</head>
<body onload="document.forms[0][0].focus()">

<form action="">
<div>
<input type="text"/>
<input type="button" value="display image"onclick="displayPic()"/>
</div>
</form>

<div><img id="pic0"src=""alt=""/></div>
<div><img id="pic1"src=""alt=""/></div>
<div><img id="pic2"src=""alt=""/></div>
<div><img id="pic3"src=""alt=""/></div>
<div><img id="pic4"src=""alt=""/></div>
<div><img id="pic5"src=""alt=""/></div>

</body>
</html>




I have put 6 images in but you can, of course, add as many as you require.
Just increment the id by 1 each time. :cool:

postitlord
10-09-2004, 02:06 AM
Beautiful!

1. It appears pressing enter in the text box clears all images. Can this be changed so that this acts like you pressed the button?

2. Is there any way to make a loop that would generate code representing:

<div><img id="pic0"src=""alt=""/></div>
<div><img id="pic1"src=""alt=""/></div>
<div><img id="pic2"src=""alt=""/></div>
<div><img id="pic3"src=""alt=""/></div>
<div><img id="pic4"src=""alt=""/></div>
<div><img id="pic5"src=""alt=""/></div>
?
I ask because I want to generate a list to 100.

3. I see var str = pattern.replace(/-/g,'');filters out the dash. Can we also include another delimiter to filter out: the period? And as an aside, I don't exactly understand the syntax /-/g, especially the g.

4. I see document.getElementById("pic"+i).style.display="block";is being used as a place holder for the image. Can this be changed to maintain its function without displaying the red x while the image is downloading? You know, because it's associated with broken images.

5. Just curious. Why is it forms[0][0]? Why not just forms? Just can't fathom how a two-dimensional array is being used.

coothead
10-09-2004, 12:07 PM
Hi there postitlord,


Don't know. I would suggest not pressing 'Enter' whilst within the text box :D
Yes. I have made the modification.
'g' performs a global match. That is, it finds all matches rather than stopping after the first match.
In this case actually it was not needed so I have removed it :D
Don't know what you mean by...filter out the period :supereek:
I cannot do anything about the 'red x'. It is an I.E. feature. I thought the 'display:none' might stop it
but it doesn't so have removed that as well.
forms[0][0]....first form, first form element. I prefer to use it instead of 'document.getElementById'.
I have added a fieldset to the form which is now the first element, so the code is now forms[0][1]
Ah, there is no 6 :D


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Display Picture</title>

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

<style type="text/css">
/*<![CDATA[*/
body {
background:#ccc;
}
div {
width:300px;
background:#fff;
border:solid 1px #000;
margin:auto;
margin-bottom:4px;
}
p {
width:300px;
font-family:arial;
font-size:12px;
text-align:center;
margin-top:10px
}
fieldset {
width:210px;
border:inset 4px #ccc;
margin:auto;
}
input {
width:100px;
text-align:center;
}
img {
width:300px;
height:300px;
}
/*//]]>*/
</style>

<script type="text/javascript">
//<![CDATA[

var i=0;
var url="http://www.radioshack.ca/images/RadioShack/25/";

function displayPic() {

var x=document.forms[0][1].value;
var y=/\d{3}\-\d{4}/;

if(x.match(y)) {
var pattern=document.forms[0][1].value;
var str = pattern.replace(/-/,'');

var elemD=document.createElement("div");
elemD.setAttribute("id","div"+i);

var elemP=document.createElement("p");
elemP.appendChild(document.createTextNode(pattern));

var elemI=document.createElement("img");
elemI.setAttribute("id","pic"+i);
elemI.setAttribute("src","");
elemI.setAttribute("alt","");

elemD.appendChild(elemP);
elemD.appendChild(elemI);

document.body.appendChild(elemD);

document.getElementById("pic"+i).src=url+str+"l.jpg";

i++;
}
else {
alert("PLEASE NOTE\n\ninputs must be of this type...\n\n251-1606");
document.forms[0].reset();
document.forms[0][1].focus()

return;
}
}

//]]>
</script>

</head>
<body onload="document.forms[0][1].focus()">

<form action="">
<fieldset>
<input type="text"/>
<input type="button" value="display image"onclick="displayPic()"/>
</fieldset>
</form>

</body>
</html>

postitlord
10-11-2004, 09:31 PM
First and foremost I appreciate all the wonderful work you've done. In its current form it's already an asset I'm going to recommend it to the higher-ups. :)

As such, I have some Internet Explorer issues to resolve. Your page works exactly as specified in Opera 7.5, but there are three distinct problems that hold back it's design from being perfect. I'm inferring IE is not your primary browser, and ask if anyone can help with these things:

Edit: Resolved. 1. Pressing enter clears all images. My code below holds it at an alert button until okayed. Opera doesn't clear the images. Unfortunately, Internet Explorer 6 is 100% of my target audience.

2. Newly entered items are appearing at the bottom. Wish I knew more about appendChild, maybe there's an inverse method out there? Opera's appending above. It's IE that's appending below. :/
When you were using <div><img id="pic0"src=""alt=""/></div> in your first example I simply reversed the order of pic0, pic1, pic2, etc, and that DID work in IE.

3. All images fetched are being stretched to 300 pixels height now. Again, Opera has zero problems, it's only IE. When I strip the height:300px and width:300px lines, the images even more bizarrely collapse to about 30 by 30 pixels!

I've added some boring stock number formatting rules. It makes entering numbers easier for us. It's almost identical to our Point Of Sale system.

Stock number conveniences:
230-0023 is the same as
23-23
230-23
23-0023
2300023, and
23023

Stock numbers for testing:
2511606 my computer :)
1616420 lowest pixel height I've seen.
1610150 low pixel height.
5800004 shortest -- three important digits.
4318488 longest -- seven important digits.
2300023

PS, I'll focus on that period thing later.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Display Picture</title>

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

<style type="text/css">
/*<![CDATA[*/
body {
background:#ccc;
}
div {
width:300px;
background:#fff;
border:solid 1px #000;
margin:auto;
margin-bottom:4px;
}
p {
width:300px;
font-family:arial;
font-size:12px;
text-align:center; /* sigh. doesn't work with b. */
margin-top:10px
}
fieldset {
width:215px;
border:inset 4px #ccc;
margin:auto;
}
input {
width:100px;
text-align:center;
}
img {
width:300px;
height:300px;
}
/*//]]>*/
</style>

<script type="text/javascript">
//<![CDATA[

var i=0;
var url="http://www.radioshack.ca/images/RadioShack/";

function displayPic() {

var pattern=document.forms[0][1].value;
///REPLACE . WITH DASH. Yes, it's hardcoded, but it's simplest my mind can handle at midnight.
///ELIMINATE ALL NON-NUMBERS MORE THAN THREE DIGITS BEFORE --. AND MORE THAN FOUR DIGITS AFTER --.
pattern=trim(pattern);

var len=pattern.length;
var dash=pattern.indexOf("-");
var gen=0;
var spe=0;

//dash used
if (dash!=-1) {
gen=pattern.substr(0, dash);
spe=pattern.substr(dash+1, len-dash);
if (gen<10){gen=gen*100}; //NEVER NEED. JUST HERE FOR STRUCTURAL INTEGRITY
if (gen<100){gen=gen*10};
}

//dash not used
else {
gen=pattern.substr(0, 3);
spe=pattern.substr(3, len);
}

spe=pad(spe,4);
//alert("dash:"+dash+" len"+len+" gen"+gen+" spe"+spe);

var dir = pattern.substr(0, 2)+"/";
//alert("pattern.substr"+pattern.substr(0, 2)+ " gen.substr(0, 2):" +gen.substr(0, 2));

var elemD=document.createElement("div"); //make
elemD.setAttribute("id","div"+i); //set id to div1, div2, div3

var elemP=document.createElement("p"); //make
elemP.appendChild(document.createTextNode(gen+"-"+spe));

var elemI=document.createElement("img"); //make
elemI.setAttribute("id","pic"+i); //set id to pic1, pic2, pic3
elemI.setAttribute("src",""); //initialize
elemI.setAttribute("alt",""); //initialize

elemD.appendChild(elemP);
elemD.appendChild(elemI);

document.body.appendChild(elemD);

document.getElementById("pic"+i).src=url+dir+gen+spe+"l.jpg";

//Attempt to link to later ex: http://www.radioshack.ca/estore/Product.aspx?language=en-CA&catalog=RadioShack&product=2300023
i++;
selecttext();
}

function pad(number,length) {
var str = '' + number;
while (str.length < length)
str = '0' + str;
return str;
}

function trim(str)
{
return str.replace(/^\s*|\s*$/g,"");
}

function checkKey(e){

if(e && e.which){ //if which property of event object is supported (NN4)
e = e;
characterCode = e.which; //character code is contained in NN4's which property
}
else{
e = event;
characterCode = e.keyCode; //character code is contained in IE's keyCode property
}

//enter pressed
if(characterCode == 13){
displayPic();
return false;
}

//escape pressed
else if(characterCode == 27){
document.forms[0][1].select()
return false;
}
else{
return true;
}

}

function selecttext() {
document.forms[0][1].select();
}


//]]>
</script>

</head>
<body onload="document.forms[0][1].focus()">

<form action="" onSubmit="return false;">
<fieldset>
<legend>DDV DSC SWG too!</legend>
<input type="text" onKeyPress="checkKey(event)" />
<input type="button" value="display image" onclick="displayPic()" />
</fieldset>
</form>

</body>
</html>

Kram
10-12-2004, 02:53 AM
not too long ago, i had a problem with the enter button and submitting a page.

I found out that in IE if there is only 1 text input field and any number of buttons, if the user hits enter the form data in that 1 field is NOT submitted, im not sure if your problem is the same one, but the way to get around it is to add another inivisble input field tha no one can see.

I had never seen this bug before, and i read up on, apparently alot of people have this problem, but what confused me is googles site only has 1 input field and their site works fine. Maybe there is javascript or something...anyways, i hope this helped in any way

postitlord
10-12-2004, 10:48 PM
I've been able to eliminate the pressing-enter-clears-images problem. I've updated my code above to reflect this. Before I had the very simple <form action="">. A little research on how JavaScript validates forms shows me I can stop it from being "submitted". Neverminding what submit actually does, I added <form action="" onSubmit="return false;">, and suppressed whatever nonsense was trying to be done automatically by the browser. Thanks for the lead, Kram.

Only images appearing in the incorrect order, and images being distorted to the wrong size remain to be deduced.

:)

coothead
10-12-2004, 11:52 PM
Hi there postitlord,

check out these modifications, I have removed some unwanted code (mine :D)
and cured the I.E. image size problem
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Display Picture</title>

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

<style type="text/css">
/*<![CDATA[*/
body {
background:#ccc;
}
div {
width:300px;
background:#fff;
border:solid 1px #000;
float:left;
margin:1px;;
}
p {
width:300px;
font-family:arial;
font-size:12px;
text-align:center; /* sigh. doesn't work with b. */
margin-top:10px
}
fieldset {
width:320px;;
border:inset 4px #ccc;
margin:auto;
}
input {
width:100px;
text-align:center;
margin:1px;
}
img {
margin:auto;
display:block;
}
/*//]]>*/
</style>

<script type="text/javascript">
//<![CDATA[

var url="http://www.radioshack.ca/images/RadioShack/";

function displayPic() {

var pattern=document.forms[0][1].value;
///REPLACE . WITH DASH. Yes, it's hardcoded, but it's simplest my mind can handle at midnight.
///ELIMINATE ALL NON-NUMBERS MORE THAN THREE DIGITS BEFORE --. AND MORE THAN FOUR DIGITS AFTER --.
pattern=trim(pattern);

var len=pattern.length;
var dash=pattern.indexOf("-");
var gen=0;
var spe=0;

//dash used
if (dash!=-1) {
gen=pattern.substr(0, dash);
spe=pattern.substr(dash+1, len-dash);
if (gen<10){gen=gen*100}; //NEVER NEED. JUST HERE FOR STRUCTURAL INTEGRITY
if (gen<100){gen=gen*10};
}

//dash not used
else {
gen=pattern.substr(0, 3);
spe=pattern.substr(3, len);
}

spe=pad(spe,4);
//alert("dash:"+dash+" len"+len+" gen"+gen+" spe"+spe);

var dir = pattern.substr(0, 2)+"/";
//alert("pattern.substr"+pattern.substr(0, 2)+ " gen.substr(0, 2):" +gen.substr(0, 2));

elemD=document.createElement("div"); //make


elemP=document.createElement("p"); //make
elemP.appendChild(document.createTextNode(gen+"-"+spe));

elemI=document.createElement("img"); //make

elemI.setAttribute("src",url+dir+gen+spe+"l.jpg"); //initialize and solves the image dimensions in I.E


elemD.appendChild(elemP);
elemD.appendChild(elemI);

document.body.appendChild(elemD);


//Attempt to link to later ex: http://www.radioshack.ca/estore/Pro...product=2300023

selecttext();
}

function pad(number,length) {
var str = '' + number;
while (str.length < length)
str = '0' + str;
return str;
}

function trim(str)
{
return str.replace(/^\s*|\s*$/g,"");
}

function checkKey(e){

if(e && e.which){ //if which property of event object is supported (NN4)
e = e;
characterCode = e.which; //character code is contained in NN4's which property
}
else{
e = event;
characterCode = e.keyCode; //character code is contained in IE's keyCode property
}

//enter pressed
if(characterCode == 13){
displayPic();
return false;
}

//escape pressed
else if(characterCode == 27){
document.forms[0][1].select()
return false;
}
else{
return true;
}

}

function selecttext() {
document.forms[0][1].select();
}
function hide() {

if(document.body.appendChild(elemD)) {
document.body.removeChild(elemD); /*removes current div only*/

}

}

//]]>
</script>

</head>
<body onload="document.forms[0][1].focus()">

<form action="" onSubmit="return false;">
<fieldset>
<legend>DDV DSC SWG too!</legend>
<input type="text" onKeyPress="checkKey(event)" />
<input type="button" value="display image" onclick="displayPic()" />
<input type="button" value="hide image" onclick="hide()"/>
</fieldset>
</form>

</body>
</html>