I am creating a Dictionary app for mobile using Java Script. I have stored Dictionary source in a separate file. My doubt is how to access that file and search for particular string using Java Script.
function dic() {
var word = document.getElementById("wo").value;
var dictionary = new Array("Java","Python","Swift","HTML","PHP");
var flag = 0;
for(var i = 0; i < dictionary.length; i++) {
if(dictionary[i] == word) {
document.getElementById("result").innerHTML = "Element Found";
flag = 1;
break;
}
}
if(flag == 0)
document.getElementById("result").innerHTML = "Element Not Found";
}
This script checks for the word in array. I want to replace the array words to a text file. Thanks in advance
I am creating a Dictionary app for mobile using Java Script. I have stored Dictionary source in a separate file. My doubt is how to access that file and search for particular string using Java Script.
function dic() {
var word = document.getElementById("wo").value;
var dictionary = new Array("Java","Python","Swift","HTML","PHP");
var flag = 0;
for(var i = 0; i < dictionary.length; i++) {
if(dictionary[i] == word) {
document.getElementById("result").innerHTML = "Element Found";
flag = 1;
break;
}
}
if(flag == 0)
document.getElementById("result").innerHTML = "Element Not Found";
}
This script checks for the word in array. I want to replace the array words to a text file. Thanks in advance
Share Improve this question edited Jun 15, 2014 at 17:09 Tobias 7,7711 gold badge29 silver badges44 bronze badges asked Jun 15, 2014 at 16:19 Dilip wkDilip wk 231 gold badge1 silver badge4 bronze badges 5- You'll have to access the file using server-side code like PHP. Once it's in the document, you can do what you like with it. – ArtOfCode Commented Jun 15, 2014 at 16:33
- Sorry, I can't use PHP. because it's for mobile. – Dilip wk Commented Jun 16, 2014 at 18:50
- PHP still works for mobile. There is almost no speed loss (depending on your code), and because it's executed on the server, the platform doesn't need to have it installed. – ArtOfCode Commented Jun 16, 2014 at 20:08
- It's Tizen app, I already tried PHP in it. It's not executing the code. There is no other way, i have to get code for Java Script only. – Dilip wk Commented Jun 17, 2014 at 2:35
-
In that case, you might want to look at jQuery's
ajax
functions to get the file into your document. As I said, once it's there you can search through it as normal. – ArtOfCode Commented Jun 17, 2014 at 14:59
1 Answer
Reset to default 4To access the file, use jQuery's .get()
function. This can also be done with XMLHttpRequests
in pure JS but jQuery will handle cross-browser patibilty for you.
To get the file:
$.get("http://www.example./path/to/file.txt",function(returnedData) {
$("#element").text(returnedData);
},"text/plain");
This will load the contents of http://www.example./path/to/file.txt into the div
with ID element
. Assuming each dictionary word is on a new line, you can then separate them into an array with the following:
var text = $("#element").text();
var words = text.split("\n");
var dictionary = new Array();
for(var i=0; i < words.length; i++) {
dictionary[i] = words[i];
}
Now you have the dictionary words in your array and can search for them with your existing code (with a couple of corrections):
function dic() {
var word = document.getElementById("wo").value;
// var dictionary = new Array("Java","Python","Swift","HTML","PHP");
// This line is unnecessary; use the dictionary var already created.
var flag = 0;
for(var i = 0; i < dictionary.length; i++) {
if(dictionary[i] == word) {
document.getElementById("result").innerHTML = "Element Found";
flag = 1;
break;
}
if(i == dictionary.length - 1) {
document.getElementById("result").innerHTML = "Element Not Found";
}
}
}
That should leave you with the right answer.
Hope this helps.
EDIT:
Part of the code in my second code sample is unnecessary and only there for clarity; you can in fact just state that var dictionary = text.split("\n");
, which will yield the same result.