Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

JS question?

Options
  • 18-07-2004 10:57am
    #1
    Closed Accounts Posts: 8,264 ✭✭✭


    I need to read in a bunch of data into a Javascript function. The only problem is that I need to do it for every page on a website. So can I have an JS include file, and I guess the data file could be an XML file. I've never really written anything in JS before, so I'm working of a book I have. So be gentle.


Comments

  • Registered Users Posts: 1,268 ✭✭✭hostyle


    Do you have access to a server-side language or is this all static html?

    If you do, then you can populate your javascript data on the server before sending the page to the client. In fact you could probably make the included JS file a server-side script.


  • Registered Users Posts: 19,396 ✭✭✭✭Karoma


    what sort of data?
    why go to any real sorta hassle-just use a cookie... (unless answer to above would suggest otherwise(security,etc))


  • Closed Accounts Posts: 8,264 ✭✭✭RicardoSmith


    I don't know what the host is, but AFAIK they don't have any access to server side scripting, otherwise I would have done this another way. I'm not building the rest of the site I just have one thing to do. What I'm doing is providing functionality to search across the site which is static HTML. I found a tool on the web jsEditor that basically builds a JS and an index file to do this. But the client didn't like the way it has a seperate search and results page. He wants the search form on every page. The way the script is built this would mean putting the index in every page. I don't want to do this. I want to have it one file on the site and I'll just include it if possible where required. The index file just has the search word/term and the link to display/go to.


  • Registered Users Posts: 1,268 ✭✭✭hostyle


    I'm prsuming you mean a "search this page" utility?

    Otherwise I don't see how it can be done efficiently unless the site never changes. and if it doesn't change I don't see a need for different code on each page.

    If it is a "search this page" utility you don't need custom code for each page. Reply for more help if this is the case.


  • Closed Accounts Posts: 8,264 ✭✭✭RicardoSmith


    Originally posted by hostyle
    I'm prsuming you mean a "search this page" utility?

    Otherwise I don't see how it can be done efficiently unless the site never changes. and if it doesn't change I don't see a need for different code on each page.

    If it is a "search this page" utility you don't need custom code for each page. Reply for more help if this is the case.

    No its a search this site functionality. But its a flat HTML site with no ASP, server side scripting. So it has to be done in Client side JS.

    The JS function has a list of keywords and phrases, that people are likely to search on. It returns on a list of pages that the search text can be found in and presents the user with a list of these pages, with a brief description of the page. The user selects the link they want, hit "go" and they are brought to that page.

    Problem is that the list is in the function so it has to be in each page. But then as the list grows as it will be manually edited to included the new content. If its on every pages then every page has to be updated with the new keywords, and links etc.

    Anyway, you can use an include file in JS so I've put the keyword list in one JS include file and the JS function in another. So theres now only one file to be mantained, and they only need to put the includes on everypage. Thats the way I would do it on an ASP site, and it seems to work ok in JS too.


  • Advertisement
  • Registered Users Posts: 1,268 ✭✭✭hostyle


    What you're doing is perfectly acceptable and probably the best method given the circumstances. Is that what you wanted to hear? :)

    Another option would be to use a free third party site search like ATOMZ or google. Both require you to display their banner or name and link beside the search but *cough* there are ways to get around this ...


  • Closed Accounts Posts: 8,264 ✭✭✭RicardoSmith


    Nah I didn't know how to do it, but now I've figure it out.

    I want to submit a text value from a HTML form on one page into the JS function on another now. Dunno how to do that.


  • Registered Users Posts: 1,268 ✭✭✭hostyle


    
    <form action="blah.html" method="GET">
    <input type="text" name="searchterm">
    <input type="submit" name="search" value="Search">
    </form>
    

    Now your new page should be blah.html?searchterm=whatever&search=Search

    You want to parse this URL when the page has loaded:
    
    <script language="JavaScript">
    
    	var searchquery = '';
    
    function getSearchTerm() {
    
    
    	var nVars = location.search;
    	nVars = nVars.substring(1, nVars.length);
    	var PassedVars = nVars.split('&');
    
    	for ( i=0; i < PassedVars.length; i++ ) {
    		parts = PassedVars[i].split('=');
    		if ( parts[0] == "searchterm" ) {
    			searchquery = unescape(parts[1]);
    		} 
    	}
    
    	// do whatever you like with searchquery
    	alert(searchquery);
    
    }
    
    <BODY onLoad="getSearchTerm()">
    
    </script>
    
    

    Hopefully that helps.


  • Registered Users Posts: 21,257 ✭✭✭✭Eoin


    BTW, the syntax for the include file is as follows:
    <head>
        <script type="text/JavaScript" src="myScriptfile.js"></script>
    </head>
    

    Then you can call the functions in the javascript file from every page that you have the tag as above.

    Eoin


Advertisement