Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

How to check a string “StartsWith” another String and one string contains another substring? in Javascript


In JavaScript ,
var carname = 'Volvo XC60'; String indexes are zero-based: The first character is in position 0, the second in 1, and so on.

How to check a string “StartsWith” another String

You can use ECMAScript 6's String.prototype.startsWith() method, but it's not yet supported in all browsers.

you can use it like this:

"Hello World!".startsWith("He"); // true

var haystack = "Hello world";
var prefix = 'orl';
haystack.startsWith(prefix); // false

Another alternative with .lastIndexOf:

haystack.lastIndexOf(needle, 0) === 0


How to check one string contains another substring

String.prototype.indexOf returns the position of the string in the other string. If not found, it will return -1.

var string = "foo",
    substring = "oo";
console.log(string.indexOf(substring) > -1);


Or some times it works with,javaScript is case sensitive.

indexof() should actually be indexOf()

Try fixing it and see if that helps:

if (test.indexOf("title") !=-1) {
    alert(elm);
    foundLinks++;
}


Open Links in New Pop off Window using Jquery


Here we want to open the links provided in the a tag in the new window.


HTML

<html>
    <head>
        <title></title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>
    </head>
    <body>
        <a href="http://www.google.com.com/" >click me</a>
        <a href="http://www.facebook.com/" >click me</a>
    </body>
</html>


Jquery  

$(document).ready(function(){ $("a").click(function(){ event.preventDefault(); window.open($(this).attr("href"), "My Task", width=600,height=300); }); })

Run it here Jsfiddle