including javascript file and w3c validation - javascript

I bought a template and in the main page, it included the javascript file as follow:
<script
src="thefile.js?v=v1.9.6&sv=v0.0.1"></script>
As you can see there are two argument at the end of the file one is v which I assume is the version and sv which I don't know what is it.
When I check the file in one of the w3c validator, It shows error and says that the " & did not start a character reference".
Now I have two questions: First what is the sv stands for and second should I remove the & in the script to eliminate the error?

The param ?v=v1.9.6&sv=v0.0.1 indicates a query string, and the browser will therefore think it is a new path from, say, ?v=v1.9.6&sv=v0.0.1. Thus causing it to load from file, not from cache. As you want.
And, the browser will assume that the source will stay the same next time you call ?v=v1.9.6&sv=v0.0.1 and should cache it with that string. So it will remain cached, however your server is set up, until you move to ?v=v1.9.7&sv=v0.0.2 or so on.
& is unrecognized char use its html code "&amp" to avoid errors as mentioned in comment

Related

How to copy certain urls from a webpage?

I have hundreds of page which I need to grab the urls of certain filehosters, but since the number of link for each page and the pages theyself, it has been tested to find the string and copy to the clipboard , using javascript
static getFileUrl(id){
var url=new URL("https://fileshosting.net/file/FileUrl")
url.searchParams.append('*',id)
return url.href
}
static copyFileUrl(id){
copy(this.getFileUrl(id))
}
I'm not to used to javascript but this should suffice, and everything it gets its the next console output
Uncaught SyntaxError: unexpected token: identifier
I understand this mean a semicolon before the statement is missing. But I don't get where it is. AFAIK there are two statement (the static procedures).
And sometimes there is only one url but in other pages there are 20 or 50 urls so perhaps it should be used an array, but I don't know how to implement an array in javascript.
so is there some method to, instead of open manually page to page, parse it from a terminal and pipe it to this (java)script or another language rutine?
Thanks in advance.
UPDATE
I have found another way, since it doesn't work this approach.
Thanks for the support and time.

How to get actual line from javascript error line number

The line # isn't so useful when your page is dynamically generated. What I'd really like is the actual offending line.
Is there some way of doing this? Perhaps
document.getElementsByTagName('html')[0].innerHTML.Split('\n')[lineNumber-1];
// OR
document.body.innerHTML.Split('\n')[lineNumber-1];
However, when I tested it in Firefox 10, I got that the above were inaccurate unless the html and body tags were on the same line.
Thanks in advance.
For anyone who is curious, the answer for me was to make the number of lines above the first head or body element consistent across all pages.
For me this magic # is three.
One should note though that if the error occurred in a js file, then I don't know how to get the actual line client-side. However, since the whole point of this was to log the line to the server (using ajax), I could simply read the line of the js file on the server. So it means that you have to ad an extra check on the url of the error to see that it is or is not a javascript file.

Why does a JS script placed after the </body> and </header> get executed?

I was working on something in PHP and I wanted to include a file and insert something at the end. Without thinking about it, I did the include and then echoed out the material I wanted to insert, which was a JS script.
When I looked at the output, I realized I had forgotten about the tags in the included file. The script was inserted after them, but surprisingly (at least to me) it was executed.
Had you asked me before I did this if a script after the and tags would execute, I would have said "I don't think so." I would have said that I thought it would not execute because I had assumed, up to now that anything after the and tags is ignored by browsers.
So, had you asked, I would have given that answer and I would have been quite wrong.
A script placed after the and tags does execute - why?
I've tried it with FF 3.6.24 and I.E 8.0.7601.17514 and it behaves the same in both.
Any text after the and tags is displayed - why?
Does anyone have any thoughts on this? And, is this something I might be able to rely upon? If so, I can simplify some processing, here and there.
Here's the page I was playing with http://www.bobnovell.com/PastHtmlEndTesting.shtml - let me know if your particular browser does not execute the script and/or display the text that I've put after the script.
Bob
This is well specified behavior in HTML5 though it will be flagged by an HTML5 validator.
The after after body insertion mode defines what happens to content that is after the </html> tag. The rule that handles this case is:
Anything else
↪ Parse error. Switch the insertion mode to "in body" and reprocess the token.
So techinically, it's a parse error, but one with well defined behavior. The <script> element is parsed and executed as if it had appeared in the body, and the element should appear in the DOM in the body.
Most browsers will not treat "parse errors" as fatal. The HTML 5 spec explains:
Certain points in the parsing algorithm are said to be parse errors. The error handling for parse errors is well-defined: user agents must either act as described below when encountering such problems, or must abort processing at the first error that they encounter for which they do not wish to apply the rules described below.

Validate RegExp in external .js file

Here's a perplexing problem.
I have a site that uses AJAX in which the primary home page loads a home.js file with core scripts that are required by all pages on the site. One of these core scripts is the following RegExp:
var datePattern = new RegExp("^([0][1-9]|[1][0-2])\/([0][1-9]|[1-2][0-9]|[3][0-1])\/([12][0-9][0-9][0-9])$");
When the user goes to a page (via AJAX), the appropriate form validation script.js file is loaded first which contains the following to validate dates (where val is the value of the date element and correctly displays the anticipated date value):
var val=elem[i].value;
if (!datePattern.test(val)) {
elem[i].style.background="#FF0000";
errno++;
}
I have passed both the script and the HTML through a validator and everything appears to be ok.
Everything works fine in FF. However in IE. I get the message that the datePattern is undefined. What is it about the datePattern declaration in the home.js file that I need to change to ensure it is available globally to other js files?
Thanks!
Scripts on a page are executed one after another. Logically, all that needs to happen is the needle-defining-line is executed before whatever needs it. On a static page it's as simple as putting it before the other JS in the markup. In AJAX it needs to be loaded and somehow executed before the other one, which may prove more difficult.
The other common problem is scope. Since you're using var, the variable will be local to its own scope. Make sure that line isn't in a function call. A good way to narrow down the problem is to remove the var keyword. If the problem is fixed; blame scope. If it persists; blame execution order.
I hope that helps with the debugging.
The original regex expression and syntax worked fine. The problem was with another jquery syntax that was failing and not reporting an error, which caused the subsequent expressions to fail.

Run time error handling on lazy loaded javascript?

Does anyone have any ideas how to do error handling on lazy loaded javascript? I am using an approach in which an ajax request is called and the code is eval'd in global scope. When a runtime error is struck, it spits out the filename as my lazy loading script and the line number is the error line plus the line number of my eval in my loading script. This wouldn't be so bad except all the javascript files get combined into modules for sections of the site. A try catch around the javascript file itself wont catch runtime errors of the functions. Any ideas? Window.onerror doesn't provide the correct filename so it is out of the question. I need to catch it before it is hit.
I was thinking maybe I could programmatically include try catches around all the functions within the eval'd code (which is ugly), but since it is done at the window level I am not sure how to access the eval'd code specifically and dynamically. Sure if the javascript is an object named "Bob" I can access window.Bob but I need to do it dynamically.
I solved the issue, however it is not the most elegant solution. Essentially what I do is this:
1. After the site loads I look at all the objects that are in window and push them into an array. This basically says to my code, ignore these objects.
When I modularize my code I keep track of the length of the files and fileNames being place into a module.
The last line of the modulizer takes the fileLength array and lineLengths and calls a function in my error handling object;
The error handling code finds new objects in window. If they exist, set a property to match fileLengths and fileNames;
Recurse through the new objects and add decorate the functions to have try catches around them.
When one of those catches is hit, traverse upward and find the properties.
Calculate the file and line number based on the properties.
Output the new error based on the correct file and line number;
Yes ugly... but it works.

Categories

Resources