Javascript global variable not working & more

I have a few questions about the code below:

  • I define a global variable called iMax on line 11. It doesn't work if I try to call it on line 17 though. Why?[ANSWERED]

  • This script won't work in web browsers due to node modules 'cheerio' and 'request' but it works in the local command prompt. Is there any way I can have another javascript that does run in a webpage to share variables with and execute this javascript to run outisde of the webpage (locally)? The two javascripts will be in the same directory. [UNANSWERED]

  • When I remove 'urls = [];' from line 3, oddly enough I get an error that says:

    request('http://forum.blockland.us/index.php?action=profile;u=137487;sa=showPosts'
    SyntaxError: Unexpected token (

    which makes no sense to me because 'urls = [];' has no purpose in the code. Any ideas why this happens? [ANSWERED]

-

1    var request = require('request'),
2       cheerio = require('cheerio'),
3       urls = [];
4    
5       
6    request('http://forum.blockland.us/index.php?action=profile;u=137487;sa=showPosts', function(err, resp, body){
7       if(!err && resp.statusCode == 200){
8           var $ = cheerio.load(body);
9           $('b:contains(...)', 'tr.catbg3').each(function(){
10              totalPages = $(this).next().text();
11              iMax = (totalPages - 1) * 15;
12          });
13      }
14    });
15    
16    
17    for(i = 0; i <= iMax; i += 15)
18    {
19      console.log(i);
20    }

Declare var iMax ={A default value your cool with } ; at the top.

Right now you're actually calling that for loop before iMax is actually declared.

"When all you have is a hammer, everything looks like a nail". You're not using the correct tech in the correct ways to achieve your goals.

The JavaScript running in the browser is running on the client's sandbox, so setting global variables on the server which would then run a node.js instance is probably impractical.

What you probably need is a lightweight web server... Node can do this for you if you wish and swallow a POST back. A simple HTTP server is one of the first Node.js tutorials. Then you can do what you wish with the POST'ed information.

Really though, a simple web-server with a scripting module which can pass off the information and run up node.js is probably the easiest way of doing this sustainably.

The other posters are correct, beware of multiple statements on a single line in JavaScript, it's considered bad practice.

You should probably define the variable before calling request: var iMax; inside the global scope or use window.iMax = (totalPages - 1) * 15;. But in fact you should use the for-loop in the request-function, because otherwise it could be needless, if you don't have set iMax.

When you remove the line with urls = []; you actually remove the semicolon sign ; as well.

That makes the next line a declaration line, meaning that everything after the line cheerio = require('cheerio'), becomes another declaration of a variable. Bad bad syntax error.

So, do remove the line urls = []; but replace the comma with a semicolon so that you have cheerio = require('cheerio');