JavaScript generate an result array from XPATH

This is a function in JavaScript that use an XPATH string to generate an Array from the results.This is very useful if you have to manipulate or use some items on a page and don’t have the id(like bots).

That is the function:

You could use it in the Terminal:

And we get all hrefs on this page in the Terminal:

Use it to store it in an Array and then write it on the page:

and we got this result:

JavaScript string contains

This is a simple function in JavaScript, I use it to check if a string contains another string.The different in this function is that the return value is the position+1.So you could use it in an If without to check the return value manually, just see my test from 1 to 3.

This is the function:

Now we make a little test to see if it work:

Seems good:

JavaScript element exists

This is a JavaScript Function to check if an element exists, I use it regularly in an If.

The function:

The test:

Our result:

We could see it works as it should.

XPATH

The XPATH(XML Path Language) is a query language from the W3C, it is used to select nodes in an XML file.

Full Path

In this example I start with “/” that say we start on root and then i navigate from element to element.

 

Anywhere

In this example I use the “//” to search in the full document to find all a nodes.(you could use the “//” behind a “/” like “/html/body/div//a” to find all elements in html body div.)

 

Attributes

you could use the “@” to find all elements wit the attribute class that is the string “secondarary”.

 

As example you could select all hrefs where the Attributes contains a string:

Or select all hrefs where the text from the URL contains a string:

 

Array

If you select all Elements you get an Array, in the example above I select the firs element from my result Array.You should avoid this, better is to navigate over the class or ID because if the Page changes a bit the XPATH don’t work any more.

 

That are some basics, if you need something special take a look into the Network or on my page.As example Web crawling in PHP or Perl.

JavaScript location

In this article I wrote about the location object in JavaScript and what are the different between them.

Host

This two prints the host name, the different between them are that only host is with the port number from the host.

as example:

and we get this:

 

Port

If you just want to get the Port you could use this:

as example:

and we get this:

Together with “location.host” and “location.port” you could get “location.hostname”.

 

Protocol

To get the Server protocol you just need this:

as example:

and we get this:

 

Href,Origin and Pathname

To get some other URL information run this:

as example:

and we get this:

Href just returns the URL with the pathname, origin just the URL without the pathname, pathname just the path behind the URL and at least the search parameter returns the parameters behind the “?”.

Apache 2.2 Browser-Caching

If you want to enable the browser cache with the new Apache 2.2, you have to enable the module rewrite first and then to restart the Apache:

Add this to the File “/etc/apache2/apache2.conf” :

This is an example config, for images I choose 2,CSS 1day and for JavaScript 2 Hours from the last access.

JavaScript Regex tester

This is a little JavaScript Regex tester, just write your Regex as example "\d" or "\w" and a test string like "123abc". It is more like an example as an real use function, but you can see how it works and handle the matches. Try it:

Matches:
Code:

JavaScript generate and delete html inputs

This generates a new text box with a unique id if you hit the Add button, but if you hit the remove button it removes the last text box.
Try it:

    The count function counts the current text boxes, I used XPATH for this and then iterate trough the results to count them.

JavaScript in word press post

Sometimes I want to post some examples about JavaScript and html, but word press removes the JavaScript so it don't work, for that I installed the JavaScript-Inline-Plugin now you could wrote your code like that: But don't forget so do this in the Text tab, and if you move back to Visual its gone... that's annoying but the best what I found ....

JavaScript replace all

This is a little example how to replace all matches with replace.

Click the button to replace "i" with "X" in the paragraph below:

Visit Microsoft!