473,474 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Redirecting the browser if a value is found in an array

57 New Member
Hi,

I have created a validation file in js that will direct users to another page if the link they clicked on is stored on the file however I need the if function contained in the file to work with arrays or a list of some sort

thnx in advance here is the code I have been working with

Expand|Select|Wrap|Line Numbers
  1. <!--
  2. function validate(url){
  3. outlink = url
  4. str0 = 'page1.htm'
  5. str1 = 'page2.htm'
  6. str2 = ''
  7. if ( outlink == str0 || outlink == str1 || outlink == str2 ){
  8. outlink = 'error.htm'
  9. }
  10. else{
  11. outlink = outlink
  12. }
  13. window.location.href=(outlink)
  14. }
  15. // -->
Aug 30 '07 #1
12 1816
phvfl
173 Recognized Expert New Member
Hi Wangers,

The way I would personally do this would be to extend the Array js object through use of the prototype object and add a contains method which returns true if the array contains the string passed in:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Array.prototype.contains=function($str){
  3. //declare a boolean set to false
  4.   var $blFound = new Boolean();
  5.  
  6. //loop through all items in the array
  7.   for (var i = 0;i<this.length;i++){
  8.     //check if the string for comparisonis in the item in the array.
  9.     if($str==this[i]){
  10.       //if it is change the boolean to true.
  11.       $blFound=true;
  12.     }
  13.   }
  14. //return the value of the boolean.
  15. return $blFound;
  16. }
  17.  
  18.  
If this is in your code then all Array objects would have a method called contains which would take a string argument and return true if it matches. Your code could then be rewritten as:
Expand|Select|Wrap|Line Numbers
  1. function validate(url){
  2. outlink = url
  3. //declare array, declare explicit size if desired.
  4. var pages = new Array()
  5.  
  6. //populate from somewhere...
  7.  
  8. //check array and if it contains outlink change the value of outlink.
  9. if (pages.contains(outlink)){
  10. outlink = 'error.htm'
  11. }
  12. window.location.href=(outlink)
  13. }
  14.  
I have removed the else statement as this does not add functionality as it just assigns the value of outlink to itself. The contains is case sensitive as it stands, if you was to add a flag for case insensitivity then this could be done. Let me know if this is the case and you struggle to do this.
Aug 30 '07 #2
phvfl
173 Recognized Expert New Member
I want case insensitivity
Quote from PM

Case insensitivity can be obtained by using a regular expression (regexp) with a i (case insensitive) flag. The other flags available on regexp is g (global) which matches all instances of a string and m which causes multiline to be used.

Anyway, to put case insensitivity in this case change the contains function to:

Expand|Select|Wrap|Line Numbers
  1. Array.prototype.contains=function($str){
  2.   var $blFound = new Boolean();
  3.   var $reg = new RegExp("^" + $str + "$", "i")
  4.   for (var i = 0;i<this.length;i++){
  5.        if(this[i].match($reg)){
  6.       $blFound=true;
  7.     }
  8.   }
  9.   return $blFound;
  10. }
  11.  
The new line creates a regular expression to test against using the match function in the if loop. The "^" forces the expression to match with the start of a line, the "$" forces the expression to march the end of the line (this forces an exact match) I hope this explains:

Pattern
Uses "est" "tes" "test"

None true true true
^ false true true
$ true false true
^ and $ false false true

Let me know if you want more info on this.

BTW this is a basic use for regexp as it can be used for much more such as testing is a string is a valid email pattern or postcode.
Aug 30 '07 #3
wangers16
57 New Member
thank you for all of your help the script works great
Aug 30 '07 #4
pbmods
5,821 Recognized Expert Expert
Changed thread title to better describe the problem.
Aug 30 '07 #5
phvfl
173 Recognized Expert New Member
displays the error page no matter what values I enter into the array even if I put no value at all in there

please assist

I put this into the populate area earlier and I was wondering whether or not I was right in doing so
Expand|Select|Wrap|Line Numbers
  1.       pages = new Array('page1.htm');
  2.       pages = new Array('page2.htm');
  3.  
You are declaring an new array on each line at the moment. To add multiple pages to the use:
Expand|Select|Wrap|Line Numbers
  1. var pages = new Array();
  2. pages[0]="page1.htm"
  3. pages[1]="page2.htm"
  4. pages[2]="page3.htm"
  5.  
Just increment the index each time. The compare function code will need to be on each page that uses it, and will return true if a match is made.

If you continue to have problems please post the javascript that you are using so that I can check through it.
Aug 31 '07 #6
gits
5,390 Recognized Expert Moderator Expert
yep ...

that's right ... but to mention: a more elegant and performant way to declare and init an array is the following way - i comment the code ... read it carefully :)

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * declaring and initializing arrays
  3.  */
  4.  
  5. // declaring the standard way out of every book :)
  6. var list = new Array();
  7.  
  8. // put elements in it
  9. list[0] = 'val0';
  10. list[1] = 'val1';
  11.  
  12. // put it together in one step
  13. var list = new Array('val0', 'val1');
  14.  
  15. // since we sometimes want to have an empty list (array)
  16. // we simply declare one ... but! we dont need to use the
  17. // brackets because js don't need to eval them when we 
  18. // don't put params in ... slightly better version is now:
  19. var list = new Array;
  20.  
  21. // it is better to use literals ... only 2 chars and js don't need 
  22. // to interpret 2 keywords ;) so: best is to use literals when having
  23. // them
  24. var list = [];
  25.  
  26. // to init values you may use the literals too
  27. var list = ['val0', 'val1'];
  28.  
kind regards

ps: the same goes for objects :)
Aug 31 '07 #7
phvfl
173 Recognized Expert New Member
yep ...

that's right ... but to mention: a more elegant and performant way to declare and init an array is the following way - i comment the code ... read it carefully :)

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * declaring and initializing arrays
  3.  */
  4.  
  5. // declaring the standard way out of every book :)
  6. var list = new Array();
  7.  
  8. // put elements in it
  9. list[0] = 'val0';
  10. list[1] = 'val1';
  11.  
  12. // put it together in one step
  13. var list = new Array('val0', 'val1');
  14.  
  15. // since we sometimes want to have an empty list (array)
  16. // we simply declare one ... but! we dont need to use the
  17. // brackets because js don't need to eval them when we 
  18. // don't put params in ... slightly better version is now:
  19. var list = new Array;
  20.  
  21. // it is better to use literals ... only 2 chars and js don't need 
  22. // to interpret 2 keywords ;) so: best is to use literals when having
  23. // them
  24. var list = [];
  25.  
  26. // to init values you may use the literals too
  27. var list = ['val0', 'val1'];
  28.  
kind regards

ps: the same goes for objects :)
You live and learn, cheers
Aug 31 '07 #8
gits
5,390 Recognized Expert Moderator Expert
You live and learn, cheers
;) yep ...all the time

but the things i mentioned are things that you encounter when making heavy use of javascript, due to building ajax-applications. sometimes you have huge amounts of data to process ... and than you search for optimizations on everything ;) ... when using js only the way most users do ... there is no explicit need to do or even to know such things ... but in case you DO know and use it that way you may ready for takeoff with more complex things ...

kind regards
Aug 31 '07 #9
wangers16
57 New Member
You are declaring an new array on each line at the moment. To add multiple pages to the use:
Expand|Select|Wrap|Line Numbers
  1. var pages = new Array();
  2. pages[0]="page1.htm"
  3. pages[1]="page2.htm"
  4. pages[2]="page3.htm"
  5.  
Just increment the index each time. The compare function code will need to be on each page that uses it, and will return true if a match is made.

If you continue to have problems please post the javascript that you are using so that I can check through it.
I have tried both your way and the way that gits mentioned however it still won't work

here is a copy of the code at it's current stage:
Expand|Select|Wrap|Line Numbers
  1. <!--
  2. Array.prototype.contains=function($str){
  3.   var $blFound = new Boolean();
  4.   var $reg = new RegExp("^" + $str + "$", "i")
  5.   for (var i = 0;i<this.length;i++){
  6.        if(this[i].match($reg)){
  7.       $blFound=true;
  8.     }
  9.   }
  10.   return $blFound;
  11. }
  12. function validate(url){
  13. outlink = url
  14. var pages = new Array;
  15. var pages = ['page1.htm', 'page2.htm'];
  16. if (pages.contains(outlink)){
  17. outlink = 'error.htm'
  18. }
  19. window.location.href=(outlink)
  20. }
  21. // -->
P.S. is this code server-side?
Aug 31 '07 #10
phvfl
173 Recognized Expert New Member
The code is javascript, so it is client side.

What is the value that is being passed in for the url variable? If there is trailing information before the filename e.g. url=location/page1.htm then it would not currently match. If this is the case then remove the "^" from the $reg declaration.

I have just been playing about with calling this. Even when the return from the contains function is false the code in the if loop was being executed. Change the if loop statement to:
Expand|Select|Wrap|Line Numbers
  1.   if (pages.contains(outlink)==true){
  2.     outlink = 'error.htm'
  3.   }
  4.  
Taking into account what Gits posted the code can be made more efficient by using it as such.
Expand|Select|Wrap|Line Numbers
  1. Array.prototype.contains=function($str){
  2.   //removed keywords for new boolean and set it to false directly.
  3.   var $blFound = false;
  4.   var $reg = new RegExp("^" + $str + "$", "i")
  5.   for (var i = 0;i<this.length;i++){
  6.     if(this[i].match($reg)){
  7.       $blFound=true;
  8.     }
  9.   }
  10.   return $blFound;
  11. }
  12.  
  13. function validate(url){
  14.   outlink = url
  15.  
  16.   //removed duplicate declaration of variable
  17.   var pages = ['page1.htm', 'page2.htm'];
  18.   alert(pages.contains(outlink));
  19.   if (pages.contains(outlink)==true){
  20.     outlink = 'error.htm'
  21.   }
  22.   window.location.href=(outlink)
  23. }
  24.  
The comments can be removed and have been included to show the changes.
Sep 1 '07 #11
wangers16
57 New Member
The code is javascript, so it is client side.

What is the value that is being passed in for the url variable? If there is trailing information before the filename e.g. url=location/page1.htm then it would not currently match. If this is the case then remove the "^" from the $reg declaration.

I have just been playing about with calling this. Even when the return from the contains function is false the code in the if loop was being executed. Change the if loop statement to:
Expand|Select|Wrap|Line Numbers
  1.   if (pages.contains(outlink)==true){
  2.     outlink = 'error.htm'
  3.   }
  4.  
Taking into account what Gits posted the code can be made more efficient by using it as such.
Expand|Select|Wrap|Line Numbers
  1. Array.prototype.contains=function($str){
  2.   //removed keywords for new boolean and set it to false directly.
  3.   var $blFound = false;
  4.   var $reg = new RegExp("^" + $str + "$", "i")
  5.   for (var i = 0;i<this.length;i++){
  6.     if(this[i].match($reg)){
  7.       $blFound=true;
  8.     }
  9.   }
  10.   return $blFound;
  11. }
  12.  
  13. function validate(url){
  14.   outlink = url
  15.  
  16.   //removed duplicate declaration of variable
  17.   var pages = ['page1.htm', 'page2.htm'];
  18.   alert(pages.contains(outlink));
  19.   if (pages.contains(outlink)==true){
  20.     outlink = 'error.htm'
  21.   }
  22.   window.location.href=(outlink)
  23. }
  24.  
The comments can be removed and have been included to show the changes.
Thanks guys for all of your help it does work correctly now on all parts of my site
Sep 1 '07 #12
gits
5,390 Recognized Expert Moderator Expert
hi ...

glad to hear you got it to work :) ... post back to the forum anytime you have more questions ...

kind regards
Sep 1 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Kepler | last post by:
How do you get the height of the client browser in IE? Both document.body.clientHeight and document.body.offsetHeight return the height of the document. If the page is long and there's a vertical...
7
by: Tom Szabo | last post by:
Hi All, What I want to achieve is to display a login screen in a certain size and appearance window with no toolbars etc. To achieve this I think I need to use redirection, but I am not sure...
2
by: Robert Oschler | last post by:
I am working on a PHP 4 app that interacts with an external authorization server. The external server does "third-party" authorization of users. So I do the following: 1) Each of my PHP scripts...
1
by: AVance | last post by:
Hi, I've come across this scenario in ASP.NET 1.1 with forms authentication where the forms auth doesn't seem to timeout correctly, nor redirect to the login page. I have done some testing, and...
4
by: deepukutty | last post by:
HI all, I am using IE(Internet Explorer) as my default browser for asp.net application development. Today i faced a strange problem. When ever an exception occured in the page ....application...
9
by: WRH | last post by:
Hello I am new to asp but I made some Jscript functions which work fine. The functions contain some strings used as a registration key for some apps. It is important that these strings not be...
2
by: Flic | last post by:
Hi, I have a basic db that I access with MySQL query browser. Everything seems fine to me but I am using this db as part of a php shopping basket and when I try to add an item I get: Notice:...
17
by: mansb2002 | last post by:
Hi, We recently moved our webserver from Win2K to Win2003. The application works fine. Only problem is that when user views a report as a PDF, IE does not show it. The following code is used to...
9
by: Jonathan Wood | last post by:
I've spent days trying to come up with a solution. I'd appreciate it if anyone can help. My site requires all users to log on. There are three different roles of users, and each user type will...
8
by: LayneMitch via WebmasterKB.com | last post by:
I'm supposed to develop a page that asks info as form values and when you hit "submit" it takes you to a page that reads the values you entered into the first page and displays those values in a...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.