Setting Dynamic Variables With Javacript
March 29, 2006 12:00 AM
Hi everyone,I just spent a while figuring out a solution to this problem and wanted to share it with the community because I couldn't find anything addressing this problem.We had a Lectora project that was going to have many (20-30) embedded links pointing to external web sites. The problem was that these URL's were going to be changing soon, with more possible changes in the future. So, we wanted to find a solution that would set all of these links to use variables as the URL's. Then, we wanted to set all of these variables externally in a file, reading them at runtime to set the most current values for the URL's, and thus pointing the links to the current site.Essentially, we wanted to externalize all of our links to one location so that we could modify them easily without painstakingly finding them one-by-one and modifying them and then having to republish the whole module.After much research and trial and error, we finally found a solution:We set up all of our variables ahead of time in Lectora. This was accomplished by clicking the menu 'Tools -> Manage Variables'. Using this manager, we added a variable for each hyperlink URL that we would need in our project. For example, we set up a variable called 'myUrl'.We next added hyperlinks that pointed to these variables that we set up. So, using the example above, we highlighted some text in a text field, right clicked and chose 'Add Hyperlink', chose 'Web Address' as the target, and typed 'Var(myUrl)' for the Web Address field.Once all of our links were pointing to the variables that we set up, we created an external .js file to hold all of our variable declarations. So, we created 'myLinks.js' with the following code:VarmyUrl.set('http://www.cnn.com');VarmyUrl2.set('http://www.google.com');etc.[/CODE]Notice how the word 'Var' precedes each variable name. 'set' is a function that sets the variable specified in Lectora to the value in the parenthesis. So, the first line sets our 'myUrl' variable in Lectora to 'http://www.cnn.com'Finally, we needed to include our .js file into our Lectora project. However, we wanted to keep it external so that we could modify the link variables, and have our links in Lectora dynamically reflect these changes.This was accomplished by embedding some Javascript code into an External HTML Object to include the .js file. We clicked the menu 'Add -> Object -> External HTML' to create an HTML Object. Inside of its properties, we chose 'Header Scripting' as the Object Type, and typed the following code in the Custom HTML box:[CODE]// -->
Discussions have been disabled for this post