/    Sign up×
Community /Pin to ProfileBookmark

Search Engine Explain!>?

Hi

I have a project on JavaScript for wednesday and I have to make a search engine, that will search through my site

I searched the web for some sources and i found a good one, but since I’m still poor with JS i can’t figure out how the codes work, and Why we use certain codes in a certain place

Here is the link for the Source:

[CODE]http://www.javascriptkit.com/script/script2/jse/jse10a.zip[/CODE]

Can someone plz explain in details the code used in file jse_search, explain how it works and why we have to write it that way and such things…

I would be really glad if I get some help here…

thnx a lot….

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@ChazzlJan 27.2009 — Heyas BlackIP,

I took a look through the codes and how it works is like this...

In [I]jse_search.js[/I], you define an array that holds a series of carrot (^) delimited strings that represent 1) a search term 2) the file name 3) a description.

[code=html]
JSE^form.html^Enter JSE in the example above ...^open source, free, client side
[/code]


In the above excerpt, [I]JSE[/I] is the search term, [I]form.html[/I] is the file that the result will link to, and the rest is the description that shows up on results page when a search is submitted.

Each segment is separated by the carrot character ^

So if you were to make a search entry for webdeveloper.com, it would look like this:

[code=php]var s = new Array();
s[0] = "JSE^form.html^Enter JSE in the example above ...^open source, free, client side";
s[1] = "JSE DOCUMENTATION^doc.txt^JSE is a client-side local site search engine. Written entirely in JavaScript 1.3 it works with browsers from Netscape 4.7 to Microsoft Internet Explorer 6.0 ...^ faq, docs, instructions";
s[2] = "Webdeveloper^http://www.webdeveloper.com^Where Web Developers and Designers Learn How to Build Web Sites, Program in HTML, Java and JavaScript and More!";
[/code]


Let me know if that doesn't make sense and I can expound further.

— Chazzl
Copy linkTweet thisAlerts:
@BlackIPauthorJan 27.2009 — Thnx a lot ChazzI ? Really useful tips...

but my real problem is with arrays, i can't get how the work...

example in the code below:

[CODE]
var cookies = document.cookie;
var p = cookies.indexOf("d=");

if (p != -1) {
var st = p + 2;
var en = cookies.indexOf(";", st);
if (en == -1) {
en = cookies.length;
}
var d = cookies.substring(st, en);
d = unescape(d);
}
var od = d;
var m = 0;
if (d.charAt(0) == '"' && d.charAt(d.length - 1) == '"') {
m = 1;
}

var r = new Array();
var co = 0;

if (m == 0) {
var woin = new Array();
var w = d.split(" ");
for (var a = 0; a < w.length; a++) {
woin[a] = 0;
if (w[a].charAt(0) == '-') {
woin[a] = 1;
}
}
for (var a = 0; a < w.length; a++) {
w[a] = w[a].replace(/^-|^+/gi, "");
}
a = 0;
for (var c = 0; c < s.length; c++) {
pa = 0;
nh = 0;
for (var i = 0; i < woin.length; i++) {
if (woin[i] == 0) {
nh++;
var pat = new RegExp(w[i], "i");
var rn = s[c].search(pat);
if (rn >= 0) {
pa++;
} else {
pa = 0;
}
}
if (woin[i] == 1) {
var pat = new RegExp(w[i], "i");
var rn = s[c].search(pat);
if (rn >= 0) {
pa = 0;
}
}
}
if (pa == nh) {
r[a] = s[c];
a++;
}
}
co = a;
}

if (m == 1) {
d = d.replace(/"/gi, "");
var a = 0;
var pat = new RegExp(d, "i");
for (var c = 0; c < s.length; c++) {
var rn = s[c].search(pat);
if (rn >= 0) {
r[a] = s[c];
a++;
}
}
co = a;

}
[/CODE]


As i see, we use first variable to get the cookies that will be saves when we open our Web, and then we use indexOf to search the strings..

I know that we declare indexOf like this indexOf(substring, index)

as i know we use subtring to search for the specific string...

at the first If, I can't understand why we use indexOf like this:

p = cookies.indexOf("d=")

from what i know, in this case we will search for substring "d=", since the subtring we are going to search will be determained from the input text, i can't get why we use "d=", then:

var st = p + 2; // why we add 2 to p

var en = cookies.indexOf(";", st);//again why we are using ";" to substring

if (en == -1) {//and use of the rest of this code

en = cookies.length;

}

var d = cookies.substring(st, en);

d = unescape(d);

var od = d;

var m = 0;

if (d.charAt(0) == '"' && d.charAt(d.length - 1) == '"') {

m = 1;

}

And plz if u can explain the use of arrays on jse_search file, i understand the rest of the code like functions and other codes...

thnx a lot
Copy linkTweet thisAlerts:
@ChazzlJan 27.2009 — The script your looking at is written very poorly for many reasons. Primarily because the variable names are too small making the script cryptic, difficult to read, and in business situations it is potentially more expensive than it has to be (if it were larger for sure)... simply because single letter variables don't tell us what the author's intentions for a variable was.

In your own programming, try to be descriptive when thinking about names for your variables.

A few comments here and there would help others work more efficiently with your code.

All javascript should be inside a function except in special cases. Currently there are many global variables in this script. It's a potential for conflicts later on.

Otherwise, the application logic is good; proper indents and structure.

Just letting you know so you can be informed and aware.


[code=php]p = cookies.indexOf("d=")[/code]
indexOf is used here to detect if the variable 'd' is defined in the cookie. If it is not, indexOf will return -1... otherwise it will return 0 if the substring is found.

[code=php]var st = p + 2; // why we add 2 to p
var en = cookies.indexOf(";", st); //again why we are using ";" to substring
if (en == -1) { //and use of the rest of this code
en = cookies.length;
}
var d = cookies.substring(st, en);
d = unescape(d);
var od = d;
var m = 0;
if (d.charAt(0) == '"' && d.charAt(d.length - 1) == '"') {
m = 1;
}[/code]


Inside the cookie, if you submit[I] Hello World[/I], the contents look like this:

[code=php]d=Hello%20World[/code]

[code=php]var p = cookies.indexOf("d=");
var st = p + 2; // why we add 2 to p[/code]

I'm going to call [I]p[/I] "pointer". It points to where d= starts, if found.

st stands for "start". so start = pointer + 2;

If the string [I]d=Hello%20World[/I] in the cookie starts a position 0, then start = 0 + 2;

[code=php]var en = cookies.indexOf(";", st);[/code]
[I]en[/I] stands for end. A semicolon doesn't exist in the cookie, so this statement will always be -1. It could be taken out or rewritten.

[code=php] if (en == -1) {
en = cookies.length;
}[/code]

So the next part, [I]end[/I] equals the length of the cookie. Since [I]d=Hello%World [/I]is the only thing in the cookie, this works. However, if you append a value to the cookie anywhere else, the script will break.

Assuming [I]d=Hello%20World[/I] is the only value in the cookie, in this example, [I]en[/I]d equals 14.

[code=php] var d = cookies.substring(st, en);
d = unescape(d);[/code]

Finally we can retrieve the string the user entered in the search box.

No clue what [I]d[/I] stands for. [I]d[/I] is the substring of 2 to 14 in [I]d=Hello%20World[/I]... therefore [I]d[/I] now holds [I]Hello%20World[/I]

[I]unescape()[/I] changes the [I]%20[/I] to a space.


That's all I have time to describe right now. Hope that helps.

— Chazzl
Copy linkTweet thisAlerts:
@BlackIPauthorJan 27.2009 — Thnx a lot man... really useful tips... Now I understand very well every part of the code u just explained.. it is simpler then it looks...

I have 10 more hours to make this project...

if u can do a "Flash Explain" to the other parts of arrays it will be really good...

however... if u can't I'm really thanksfull 4 what u've done so far...

thnx a lot again!..?
×

Success!

Help @BlackIP spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...