473,698 Members | 2,235 Online
Bytes | Software Development & Data Engineering Community
+ 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 1833
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

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

Similar topics

12
10163
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 scrollbar, you get the height of the entire document, screwing up any chance of centering a window in the browser using these values. Is there a way to get the height of the actual browser window and not the entire page height? Thanks.
7
1166
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 how or what is the best way? The problem I am facing, that the user may get to the login page from different pages, or simply from typing in the address of the login page.
2
3121
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 has an include file (require_once) that checks to see if the current user has recently been authorized. 2) If not, the user is "handed off" to the external server. I do this by building the necessary URL for authorization and using refresh to...
1
2189
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 I believe I've found a solution, but I would like some insight from Microsoft on whether the code I've implemented is correct, and why it is even working. Here is my scenario:
4
2180
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 is not redirecting to the default error page which will normally come. instead page is redirecting to "http://help.internet-optimizer.com/?js=1&e=ERR500&u=http%3A//localhost/buzz/WebForm1.aspx&cls=wsi25&rid=c01"
9
3342
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 visible to a client using a browser. My question is...can a knowledgeable browser user view Jscript source code in an asp file?
2
3192
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: Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '>function.extract]: First argument should be an array in functions.inc.php on line 31
17
9464
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 redirect: Response.Redirect("/website/pdffiles/myreport.pdf"); Response.End; IE opens a "File Download" box and if you click the "open" button then
9
2334
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 have access to a completely different set of pages. "Client" pages are in the root folder and "Admin" and "Trainer" pages each have their own subfolders. The problem is that when a user goes to the domain, I want to redirect "Admin" and...
8
2060
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 message. I can't seem to find where I'm going wrong. Here's the code: Setting Cookie and Form Page: <html><head><title>Problem3</title> <script type="text/javascript" src="core.js"></script> <script type="text/javascript">
0
8672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9156
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8892
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8860
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4361
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.