itsjareds 29 Junior Poster
itsjareds 29 Junior Poster

You could also try:

<html>
<body>
<script type="text/javascript">
function notAQuote(e) {
	var keynum;
	var keychar;
	var numcheck;

	if(window.event) // IE
		keynum = e.keyCode;
	else if(e.which) // Netscape/Firefox/Opera
		keynum = e.which;

	var notQuote;

	switch (keynum) {
		case 34:
			notQuote = false;
			break;
		case 39:
			notQuote = false;
			break;
		default:
			notQuote = true;
	}

	return notQuote;
}
</script>

<form>
<input type="text" onkeypress="return notAQuote(event)" />
</form>

</body>
</html>
Xessa commented: thank you. +2
itsjareds 29 Junior Poster

Try this:

onBlur="isNumeric(this.value)"
itsjareds 29 Junior Poster

Is your problem not getting the function to work, or that PHP is not echoing the <script> tag?

It seems that you don't have correct closing brackets in your AnalyseFaultCheck() function. Here's how it looks spaced out:

function AnalyseFaultCheck(){
	var reasonDropdownList=document.getElementById("whatisthereasonforfaultslippage");
	var statusDropdownList=document.getElementById("status");
	var updateButton=document.getElementById("update");
	var proposedActions=document.getElementById("proposedactions");

	var userInfo='';



	if("Duplicate TR"==reasonDropdownList.options[reasonDropdownList.selectedIndex].value && proposedActions.value=='')){
		userInfo="Fill in TR num of original TR";
		return userInfo;

		statusDropdownList.disabled=true;
		updateButton.disabled=true;

	}

	if("Duplicate TR"==reasonDropdownList.options[reasonDropdownList.selectedIndex].value && proposedActions.value!='')){
		userInfo="Fill in TR num of original TR";
		return userInfo;

		statusDropdownList.disabled=false;
		updateButton.disabled=false;
	}

For one thing, in your conditional statements, the codes

statusDropdownList.disabled=false;
updateButton.disabled=false;

Won't run because they are placed AFTER the return statement. You should always place the return statement as the last line of your function because it halts the function processing and does not process the rest of the function. It just ends the function and returns the value.

Also, you don't have an end bracket for the entire function. Do you change what's in userInfo ? Otherwise, you don't have to change the variable in conditional.

You also added one too many parentheses in your conditionals.

Try changing your code to this:

function AnalyseFaultCheck(){
	var reasonDropdownList=document.getElementById("whatisthereasonforfaultslippage");
	var statusDropdownList=document.getElementById("status");
	var updateButton=document.getElementById("update");
	var proposedActions=document.getElementById("proposedactions");

	var userInfo="Fill in TR num of original TR";

	if("Duplicate TR"==reasonDropdownList.options[reasonDropdownList.selectedIndex].value){
		if (proposedActions.value == '') {
			statusDropdownList.disabled=true;
			updateButton.disabled=true;
		}
		else {
			statusDropdownList.disabled=false;
			updateButton.disabled=false;
		}
		return userInfo;
	}
}
itsjareds 29 Junior Poster

Yep, basically Airshow's method was much cleaner and involved writing no regular expressions. The way I went about it just re-invented the wheel -- I didn't know about the other way :P. I would advise you to replace my code with his, since his is much less (if not at all) error prone.

So, change my line:

document.location.href.replace(/(?:#(?![^#]*\.)).*$/, "") + "#jump";

To:

document.location.hash = "jump";

Both would be fine as long as your page isn't similar to facebook where it navigates through hash urls (does not change the page but adds #/newPage.html to the URL). Good luck with the rest of your site :)

itsjareds 29 Junior Poster

Try this:

<html>
<head>

<script type="text/javascript">
function randomize(min, max) {
	if (!min)
		min = 0;
	if (!max)
		max = 1;
	return Math.floor(Math.random()*(max+1)+min);
}
function randomBg() {
	var bgs = new Array();

	// I took these images from a site with a similar script already only to get example images
	// I wrote this script myself
	bgs.push("http://javascript.internet.com/img/backgrounds/wood.gif");
	bgs.push("http://javascript.internet.com/img/backgrounds/paper.gif");
	bgs.push("http://javascript.internet.com/img/backgrounds/clouds.gif");
	bgs.push("http://javascript.internet.com/img/backgrounds/faces.gif");
	bgs.push("http://javascript.internet.com/img/backgrounds/marble.gif");

	document.body.style.backgroundImage = "url(" + bgs[randomize(0, bgs.length-1)] + ")";
}
</script>

</head>
<body onLoad="randomBg()" style="text-align:center;">

<span style="font-size:1.5em; background-color:white; border:1px dashed black; padding:5px;">A random background image should appear!</span>

</body>
</html>
itsjareds 29 Junior Poster

Ah, I didn't know there was such a thing as location.hash. I think your method is better :P

itsjareds 29 Junior Poster

The anchor tag will actually work, you just don't know how to implement it.

Add this tag INSIDE your AJAX <object> element.

<object height="405" width="660">
	<a style="padding-right: 660px; position: relative; bottom: -15px; visibility: hidden;" name="jump">Video</a>
	<!-- The rest of your params and embed code -->
</object>

Then, when you use AJAX to load a video, add this to your JavaScript:

document.location.href = document.location.href.replace(/(?:#(?![^#]*\.)).*$/, "") + "#jump";

The reason for the extra regular expression is so that it removes the current anchor with the new one, and you don't get a URL searching for <a name="jump#jump">

Spycat commented: Thanks for the help - I appreciate it :) +4
itsjareds 29 Junior Poster

Well I'm not exactly sure how to do it -- but if you need a temporary solution until someone else strolls upon this thread with an answer, you can always ask your clients to archive the .mp3s into a .zip file, then upload the .zip. There's probably a PHP snippet on the web that can extract files from an archive once it's uploaded. That way, the client uploads a .zip and your server automatically decompresses it into a new folder with each individual .mp3 file.


edit: Found this link on Google: http://www.phpeasystep.com/phptu/2.html

Here is my google search, there were some other good links that I didn't read.

itsjareds 29 Junior Poster

I have never personally scripted a file upload form, but I would imagine that it is done by giving each <input type='file'> an incremental name like name=upload1 and so on. Then, in your PHP (or other language), you use a for loop to go through each uploader and process it.

itsjareds 29 Junior Poster

Here is an example script that can help you learn Regular Expressions.

<html>
<head>
<script type="text/javascript">
function removeSlash(string, searchFor) {
	// This searches for all occurrences of searchFor that occurs more than once
	// {2,} means 2 or more repeats
	// The second param, "i", marks the search case-insensitive (if you need to remove letters)
	// If you want to remove ALL multiple occurrences, change the second param to "gi"

	var RegEx = new RegExp("(" + searchFor + "){2,}", "i");
	return string.replace(RegEx, searchFor);
}
</script>
</head>
<body>

<script type="text/javascript">
var myString = "a////b//c//d";
document.write("Original string: " + myString + "<br/>");

var fixedString = removeSlash(myString, "/");
document.write("Fixed string: " + fixedString); // Should return "a/b//c//d"
</script>

</body>
</html>
aashishn86 commented: thanks... +1
itsjareds 29 Junior Poster

No problem :P

I just worked on the script, now it will work completely. It also shows regular text digits if the image you link to does not exist.

<!-- This has to be placed at the top to make these elements available to the script -->
<span id="counter"></span><br/><br/>
<input id="stopBtn" type="submit" onClick="stopCounter()" value="Stop the counter"/>

<script type="text/javascript">
var first = 42;
var number = 0;
var full_default = 7;
var numString = "";

function counter(start) {
	var count = document.getElementById('counter');
	if (!start)
		number++;
	var numString = number.toString();

	var length = numString.length;
	full = full_default - length;

	count.innerHTML = '';

	for (var i=0;i<full;i++){
		count.innerHTML += '<img src="images/0.gif" border="0" alt="0"/>';
	}

	for  (var i=length;i>0;i--){
		var curNum = numString.substring(length-i, (length-i)+1);
		count.innerHTML += '<img src="images/' + curNum + '.gif" border="0" alt="' + curNum + '"/>';
	}
	if (start)
		return setInterval("counter()",1000);
}

var stop = counter(true);

function stopCounter() {
	var stopBtn = document.getElementById("stopBtn");
	window.clearInterval(stop);
	stopBtn.value = "Counter stopped";
	stopBtn.disabled = true;
	return false;
}
</script>

Just ask if you need anything explained :D

itsjareds 29 Junior Poster

There were quite a few things wrong with you script, but I think I fixed them all. It's hard to tell when I don't have the images, but maybe you could try it out.

First, you don't have to declare a declared variable more than once. So when assigning a variable a value, you don't have to include the "var" keyword when it's already declared.

So, this:

var first;
	var number;
	
	if (first == ""){
		var first = 42;
		var number = 0;
	}

Can be changed to this:

var first;
	var number;
	
	if (first == ""){
		first = 42;
		number = 0;
	}

Also, the line number = number++; can be changed to number++; .

The reason there seemed to be continuous loading on the page was because you used document.write. I'm not exactly sure why this happens, but the page will be in continuous load when you use that function. Just take it out and the script won't do that.

Change both your loops to this:

for (i=0;i<full;i++){
		document.getElementById('counter').innerHTML += '<img src="images/0.gif" border="0">';
	 }
	 
	 for  (i=0;i<length;i++){
		document.getElementById('counter').innerHTML += '<img src="images/' + number.toString().substring(i,1) + '.gif" border="0">';
	 }

Another problem was that your <span> tag had no id labeled "counter", but you named the class attribute that. I changed your span element to this: <span id="counter"></span> One thing that I wasn't sure of was these lines:

var first;
	var number;
	
	if (first == ""){

If you're checking to see if first has …

Ezzaral commented: Very helpful posts +21
itsjareds 29 Junior Poster

The basic line that will stop the form from processing is adding this in your JavaScript if the validation failed:

if (validationFailed)
  return false;

Only return true if the validation is true, if it fails, then return false.

itsjareds 29 Junior Poster

The problem is line 22. You have a string that doesn't terminate (a quote isn't closed). Looks like you're used to another language like PHP that connects strings and variables like "string text".variable."string" . Javascript uses the plus sign (+) instead of the period to join strings and variables.

I made a few changes to that line that make it a bit more efficient. Instead of writing

document.getElementById('counter').innerHTML = document.getElementById('counter').innerHTML + (text here);

You could write

document.getElementById('counter').innerHTML += (text here);

I found another error on that line - you wanted to do this:

document.getElementById('counter').innerHTML += document.write('<img src="images/' + number.substring(i,1) + '.gif" border="0">');

This would make your <span> tag look like this:

<span id="counter">document.write(<img src="images/' + number.substring(i,1) + '.gif" border="0">)</span>

Which would simply display as plain text. You want to take out the document.write() function and just make the innerHTML equal to <img src="images/' + number.substring(i,1) + '.gif" border="0"> So, overall, change line 22 to this:

document.getElementById('counter').innerHTML += '<img src="images/' + number.substring(i,1) + '.gif" border="0">';

Another error was the line that looks like this:

var length = strlen(number);

strlen() is a php function, but is not a JavaScript function. Instead, use the .length property.

var length = number.toString().length;

I had to convert it to a string because the .length property only works on strings. This does not affect your number variable.

Could you post the link to your page? It would be helpful to see it in action.

itsjareds 29 Junior Poster

I found a few things wrong. First, since you put my code into a function, the function has to be called before it runs. This means that you have to add it to the onload attribute.

If you take the script out of the function check(), you don't have to call it with onLoad. But if you still need the function for later during runtime, you can leave it as is.

Also, when you make the url, you don't have to include quotes around 'div2'. You can leave it as ?run=div2 and it will work.

Here is a cleaned up version of your script:

<html>
<head>
<script type="text/javascript">
var url = document.location.href;
var queryVar = "run";
var regExp = new RegExp("(\\?|&)" + queryVar + "=[\\w\\d]*", "i");

function check() {
	if (regExp.test(url)) {
		var query = regExp.exec(url)[0];
		var value = query.substr(queryVar.length + 2);

		if (value == "div2")
			switchPic("div2");
		else if (value == "div3")
			switchPic("div3");
	}
	function switchPic(n) {
		document.getElementById(n).style.display="block";
	}
}
</script>
</head>
<body onLoad="check()">

<div class="picture" id="div2" style="display:none;">HI</div>

<a href="test_new.html?run=div2">Click Here</a>

</body>
</html>

Do you want to use the switchPic(n) function other times during runtime? You might want to take it outside of the check() function and make it a global function, like so:

var url = document.location.href;
var queryVar = "run";
var regExp = new RegExp("(\\?|&)" + queryVar + "=[\\w\\d]*", "i");

function check() {
	if (regExp.test(url)) {
		var query = regExp.exec(url)[0];
		var value = query.substr(queryVar.length + 2);

		if (value == "div2")
			switchPic("div2");
		else if …
itsjareds 29 Junior Poster
  1. The script can go into your page as-is without waiting for the page load. So you could just insert it into the <body> or <head>.
  2. No, you don't have to include &foo=bar at all. That was just to show that you could have other variables without it affecting the script.
  3. I don't know how to mark this thread as solved, sorry.
itsjareds 29 Junior Poster

No problem, I was still editing it, so some new stuff may have appeared.

itsjareds 29 Junior Poster

Sure. Just make another if statement.

var url = document.location.href;
var queryVar = "run";
var regExp = new RegExp("(\\?|&)" + queryVar + "=[\\w\\d]*", "i");

if (regExp.test(url)) {
	var query = regExp.exec(url)[0];
	var value = query.substr(queryVar.length + 2);

	if (value == "myFunction")
		myFunction();
	else if (value == "otherFunc")
		otherFunc();
}

Change queryVar to another word if you want to change the ?run= to anything else. For example, if you change it to load , it makes the URL search for ?load= .

The variable value is a string that is the value of ?run=VALUE HERE . Add a new if statement to run a new function.

* note: When modifying my script, make sure you don't make it run whatever function is in the URL. This could open some dangerous security holes that would allow anyone to run any JavaScript call.

itsjareds 29 Junior Poster

This is completely possible, although it would be more useful to do this server-side (PHP) rather than client-side (JavaScript).

Here is an example of how to do it in JavaScript anyway, if you have limited resources. This assumes that your URL uses a variable called "run", which causes the script to load if run=1. http://www.example.com/index.php?run=1&foo=bar

var regExp = /(\?|&)run=./i;

if (regExp.test(document.location.href)) {
	var run = regExp.exec(document.location.href)[0];
	var value = /.$/.exec(run)[0];
	if (value == 1)
		myFunction();
}
itsjareds 29 Junior Poster

This can easily be done using the split() method.

Here is an example:

function splitNames(names) {
	return names.split("/").length;
}

var names = "aashish/ankush";
alert("There are " + splitNames(names) + " names in the names list.");

The split() function has one parameter, separator , which defines the character(s) that you want to divide the text between. You could change this to anything, such as #*# so that your names string is:

aashish#*#ankush

itsjareds 29 Junior Poster
if(arguments.length == 3){
  var result = "<" + arguments[1] + ">";
  for (var i = 0; i < arguments[2].length; i++){
   result += "<li>" + arguments[2][i] + "</li>";
  }
  result += "</" + arguments[1] + "l>";
  document.getElementById(arguments[0]).innerHTML = result;
 }
}

It looks like this is creating an unordered list. arguments must be an array that contains an HTML element id name in arguments[0], an HTML tag name in arguments[1], and a list item label in arguments[2].

The part of the code if(arguments.length == 3) says that there should only be 3 elements in the array, arguments[0], [1], and [2].

I'll provide some example definitions for the variables to show you how it works.

var arguments = new Array("menu", "ul");
arguments[2] = new Array("List item 1", "List item 2", "List item 3");

if(arguments.length == 3){
  var result = "<" + arguments[1] + ">";
  for (var i = 0; i < arguments[2].length; i++){
   result += "<li>" + arguments[2][i] + "</li>";
  }
  result += "</" + arguments[1] + ">";
  document.getElementById(arguments[0]).innerHTML = result;
 }
}

will produce:

<ul>
	<li>List item 1</li>
	<li>List item 2</li>
	<li>List item 3</li>
</ul>

and put the result into the element in the HTML labeled "menu".


If you want to see this in action, copy the script on this page and paste it into Tryit editor.

itsjareds 29 Junior Poster

The best way to do this is to use Regular Expressions (RegExp). This is a useful, shorthand way to search a string for a certain value.

Here's a regular expression to search for a word in JavaScript. Regular Expressions work in most programming languages, so if you're using another, say so and I'll change it.

var word = "Dani";
var url = document.location.href;
var regExp = new RegExp(word, 'i');

if (regExp.test(url))
	alert("The word " + word + " is in the URL!");
else
	alert("The word " + word + " is NOT in the URL.");

Change the variable word to whatever word you need to search for.