![]() |
| ||
| Javascript: Multiple Code Blocks Hi, folks. I have written some pretty complicated programs in Javascript (in terms of the code itself), but I haven’t experimented much with the nuts-and-bolts of Javascript. In particular, I have written all my programs as single blocks of code. For example, I have written several programs to solve for the eigenvalues and eigenvectors of matrices. One page solves for a 3 x 3 matrix, one for a 4 x 4 matrix, one for a 5 x 5 matrix, etc. Each javascript program is on its own page and takes the following form: <script language="javascript" type="text/javascript"> The algorithm for actually calculating the eigenvalues and eigenvectors does not change depending upon matrix size, it is included in the BIG BLOCK OF CODE. What I am hoping to do now is turn the code block for the BIG BLOCK OF CODE into an external file, and have the several programs refer to it. That way, I won't have six programs include the same big block of code; they can all refer to the same external file--saving space on the server. For example: <script language="javascript" type="text/javascript"> <script language="javascript" type="text/javascript" src="RoutineforEigenvalues.js"> function A_Smaller_Program_Output(dataForm){
Is what I want to do possible? How seamlessly does Javascript transmit data between seemingly independent code blocks? |
| ||
| Re: Javascript: Multiple Code Blocks You have the right syntax. Just save your program as eigenValues3.js and then if you want to write a program that needs that file you just "import" it as follows: <html> Note: 1. You do not have to do the "import" within the <head> tag. It could have done withing the <body> tag. 2. <script> tags that are used to import files cannot enclose javascript code (or at least this was the case last time I tested this about 1yr ago). That's why I have two <script> tags above. The first one to import the file. The second to write my code for my local/current page. 3. You do not need language="javascript". Providing type="text/javascript" will suffice. |
| All times are GMT -4. The time now is 3:54 am. |
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC