Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Single String

Status
Not open for further replies.

bumpies1

Programmer
Sep 5, 2008
3
US
How would I form this js into a single string?

<script type="text/javascript">
function SearchForIt()
{
var searchWord = document.getElementById("searchWord").value;
var searchSelection = document.getElementById('searchSelection').value;
if(searchSelection != "")
{
window.open(searchSelection + escape(searchWord));
}
if(searchSelection == "")
{
document.getElementById('errorSelect').innerHTML = 'Please select a site to search!';
}else {document.getElementById('errorSelect').innerHTML = "";
}
}
</script>
 
Here is how I would approach this...

Starting with your original function, rewrite it to be functionally the same (but easier to understand) - indenting helps at this stage:
Code:
function SearchForIt() {
	gEBId = document.getElementById;
	searchSelection = gEBId('searchSelection').value;
	if (searchSelection == "") {
		gEBId('errorSelect').innerHTML = 'Please select a site to search!';
	} else {
		window.open(searchSelection + escape(gEBId("searchWord").value));
		gEBId('errorSelect').innerHTML = "";
	}
};

This can then be rewritten to reduce the amount of physical lines:
Code:
function SearchForIt() {
	gEBId = document.getElementById;
	gEBId('errorSelect').innerHTML = (searchSelection == "") ? 'Please select a site to search!' : '';
	if ((searchSelection = gEBId('searchSelection').value) != "") {
		window.open(searchSelection + escape(gEBId("searchWord").value));
	}
};

And then all the unnecessary whitespace is removed to leave it on one line:
Code:
function SearchForIt(){gEBId=document.getElementById;gEBId('errorSelect').innerHTML=(searchSelection=="")?'Please select a site to search!':'';if((searchSelection=gEBId('searchSelection').value)!=""){window.open(searchSelection+escape(gEBId("searchWord").value));}};

Note that you have no error checking in the function. If the elements with those Ids do not exist in the DOM, then a javascript error will be thrown.

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
For reasons beyond my control (ugh), I've been asked to rewrite the js into a single string within my page. So I was starting to do something like onkeypress="if(even.keyCode == 13){window.open...
**********************Code Below*************

<label>Search</label>
<input class="titleSmallTD" name="searchWord" type="text" id="searchWord" />
<select id="searchSelection" style="width:155px;" onkeypress="if(event.keyCode == 13){window.open
<option value="">Select a Site</option>
<option value="">google.com</option>
<option value="">dictionary.com</option>
</select>
<input name="Search" type="button" class="required" value="Search" onclick="window.open
<div id="errorSelect" style="font-weight:bold;color:#FF0000;"></div>
***************************************

But I wasn't sure how to form the js into one line of code.
 
Jeff - Yes, thank you!! You got me pointed in the right direction and I'm adding in some error checking.

Now I've been tasked with getting the js to run from within the html of the page with something like an onkeypress or onclientclick event. Any suggestions on how to get that string started with something like onkeypress="if(even.keyCode == 13){window.open...
 
I would write a common function that is then called when, say, the keypress event is triggered:
Code:
<script type="text/javascript">
function checkKeyPress(eventData) {
  var theKeyCode = (window.event) ? eventData.keyCode : eventData.which;
  var theCharacter = String.fromCharCode(theKeyCode);
  if (theCharacter == 'x') {
    alert('You just typed in the letter "x"! That is the best letter.');
  }
}
</script>
...
<input type="text" id="typein" value="" onkeypress="checkKeyPress(event)"/>
...
The code above will alert a message when a lowercase 'x' key os pressed.

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top