Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Saturday, January 22, 2022

How to Get Letter Only Using JavaScript





 step 1: open dilsecodie

 step 2: Goto Js section Letter Only regex

 step 3: get copy code in your function and get the result on a letter bypassing any word.


To get an only letter from the given string 

using jquery validator 

suppose to remove a number or any special char need to use

regex /^[a-zA-Z ]*$/; can allow only letter.


JavaScript was designed to add interactivity to HTML pages

JavaScript is a scripting language that reflects the object orientation of web pages.

It can be used for both client side & server side applications.

Client side java script is typically executed in a browser that supports the language.

It helps in the creation of HTML pages capable of responding to user events like mouse clicks, key presses, and selection of elements in the form.


Validation can be done on frontend (JavaScript) or on backend(php or any backend language) or both.


For example in an input box for mobile number you would not want alphabets since a mobile number is always a number. By using validation on frontend you avoid sending incorrect information to the server.



It would be /^_.*/


It will match any string that starts with underscore. Forgot to mention, it won't match newline inside the string.


To match a newline too in a string you can append a single line modifier and new regex will be /^_.*/s

"\" in a string, this depends on the quote you use,

 either single or double. If you’re making use of a double quote, 

escape characters can be used to do so as in the case below;


var x = "\"\\\""; 

console.log(x); 

The code snippet below will print “\” in the console.


Whereas, if it’s a single quoted string, you use  '"\\"';  simply.



^ % | & etc. That means that it is too a special character that can be escaped by \ 

I think the expression above matches a single character that is NOT present in

a-z

A-Z

the character "-"

whitespace



A x-y matches any character in the range from x to y, according to the collating order of the character encoding.

A x matches x.

A \ starts an escape sequence. There are two kinds of escape sequences: one that removes a special meaning from the following character, and one that imparts a special meaning to the following character. In the above example both are used: \- removes the character range meaning of -, so \- means "the character - itself"; the other case above is \s, which imparts a special meaning to s, which is "any character considered a spacing character".

And finally, any sequence of the above cases is just added together in a single character class



"One or more" is a common thing, so has a special symbol: “+”. You can replace “{1,}” with “+” in your regex without changing what it means.


Perhaps(?) the spaces aren’t actually in your regex, and you added them to make things clearer. In that case, your regex could be expressed as “[^.]+”, and would be “one or more non-periods.”



Note* If you are a subject expert and want to contribute to the internet community, welcomes you to write your own tutorial and share with us, if we found them accurate and relevant we will publish them on our website.

care@dilsecodie.com


How to Get Url from website Using Javascript

                                        


 step 1:  open dilsecodie

 step 2: goto Js section copy code in your function .

step 3: Get Url from the page. check code 

step 4: pass complete URL  in function 

step 5: get the result in the return type 


So common script can be used like :

var url = window.location.href; - its provide browser URL  

var index = url.indexOf("%20"); - to removed encoded keyword

while (index != -1) {  break URL into part 

url = url.replace("%20", " "); - get original URL from  it .

index = url.indexOf("%20"); - get index of word

}

var results = new RegExp('[\\#?&]' + name + '=([^&#]*)').exec(url); - remove unnecessary part

return results[1] || 0; - return your result .

var pagepath = window.location.pathname;

var websitename= window.location.origin; 

also can use these window method to get some results.


So lets see other ways to get URL from browser .

var URL_jq =  $(location).attr('href'); - using jquery method to get URL .

var pagepath_jq =  $(location).attr('pathname'); 

var websitename=  $(location).attr('origin'); 

var portjs = $(location).attr('port');    - get url port  

var protocoljs =$(location).attr('protocol'); - get url using protocol.


$.getJSON("dilsecodie.php?callurl=?", -function used to send request.

 { url:$(location).attr('href')}, - to get url hostname.

function(jsonresult){ }); function used  to perform json responses.

var hosturl = $(this).attr('href');  - using this jquery script can change url parameter

location.hostcode = hostcode; - its will pass current value in it .

Promise.all([ -    fetch data from url in array

  fetch('https://dilsecodie.com/api/').then(req=> req.json()), - send request to direct get url and save       response in array 

  fetch('https://dilsecodie.com/api/').then(req => req.json())

 

]).then(console.log) .

above script used to get data from multiple  URLs and the response will be stored in an array.

To get the current URL use javascript window.location.href property to get complete URL.

for jquery XMLHttpRequest object method used to send URL to backend

document.baseURL its also away to get baseurl from this script


Some javascript property to get URL components.

href - to get complete URL

host - is used to get hostname with port 

protocol - to get URL using protocol

hash - is used to hash portion of URL.

port - is used to get port from URL

search mainly used to get URL query parameter 

Now can see other scripting languge how to get url from browser using php

$_SERVER['QUERY_STRING'] - its used when query parameter with url 

$_SERVER['REQUEST_URI'] - this script used when completed url required


echo 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];  can used to full path with url


if possible these script didnt work thenyou can use

$_SERVER['HTTP_REFERER']  to get whole url .


for react js if need complete url then for that script  will be

class dilsecodieApplication extends React.PureComponent {

  

  render() {

    this.props.location.pathname - its provide you the pathname of url 


}

then pass this url to component - for thatb  useParam hook to access get values in your url.

and pass that in routes block 

<Route path ="pathfrom url" exact ={} component ={component} />



How to Use Javascript Function






 step 1: open link dilsecodie.

 step 2 : Javascript section Splitword Function Using Javascript.

step 3: copy code from the website 

step 4 : use in any Js function

step 5: only pass the word, need as a split result, dynamically 

step 6 : get the return from the function 


Now how to check cookies in javascript .

var cookies = document.cookie ? document.cookie.split('; ') : [];

var rdecode = /(%[0-9A-Z]{2})+/g; var i = 0 for (; i < cookies.length; i++) {

var parts = cookies[i].split('=');

var cookie = parts.slice(1).join('=');

          if (cookie.charAt(0) === '"') {cookie = cookie.slice(1, -1);}

Using this piece of code in your project you will get cookie status in your project .

Like doing validation of form  if you want to validate any HTML form we can do it using javascript function  .

function validateForm() { 

    var x = document.forms["myform1"]["demo"].value;

    if(x==null || x ==''){ 

    }else{

    $("#loader").show(); --- its a custom way to show some msg or loader at user end.

    return true; -- checking for status 

    }

}


Now lets see inner HTML example bcoz its required every where when using javascript ,

var ele = document.getElementById(id);

if(ele.style.display == "block") {       

    ele.style.display = "none";

    document.getElementById("header").innerHTML = "Dilsecodie Search";

    document.getElementById("search").value = "dilsecodie blog";

   

}else{ 

ele.style.display = "block"; 

document.getElementById("header").innerHTML = "dilsecodieSearch";

                        document.getElementById("search").value ="dilsecodie";

}  

as we can see javascript is a way we can use innerHTML to set or provide any values to particular elements by using id ,

as you can also see in the example we can also reset the style property for that element by using its id.

Now let's assume how to create an HTML page using javascript.

Create a new HTML document.


var writehtmlDoc = writehtml[0].contentWindow ? writehtml[0].contentWindow :

 writehtml[0].contentDocument.document ? writehtml[0].contentDocument.document : 

writehtml[0].contentDocument;

        writehtmlDoc .document.open();

      writehtmlDoc .document.write('<html><head><title>DIV Contents</title>');

        var htmlToPrint = '' +

        '<style type="text/css">' +

        '.table3 th, .table3 td {' +

        'border:1px solid #000;' +

        'padding:0.5em;' +

        '}' +

        '</style>';

        writehtmlDoc .document.write('</head>');

        writehtmlDoc .document.write(htmlToPrint);

        writehtmlDoc.document.write('<body>');

        writehtmlDoc.document.write(contents);

        writehtmlDoc.document.write('</body></html>');

        writehtmlDoc.document.close();

        setTimeout(function () {  window.frames["writehtml"].focus();

            window.frames["writehtml"].print();--- some custom function

            writehtml.remove();

        }, 600);


From the above  example, we can see how  we can create every HTML document, 

first created headers for page, as you can also see how div created you can create n numbers

of div according to your page design, these types of pages are also used for cloning for pages using

javascript code.


Now let's see some important functions of javascript .

 how to make page open in new window 

function makenewwindow(){  - this function  used to make new window with output page

var strwidth=window.outerWidth-650; -- to set width of a page.

var strheight=(window.outerHeight-160); --- need to set your height of page as per requirement .

var leftStr = (screen.width-window.outerWidth)/2; -- its calculate left side window

var topStr = (screen.height-window.outerHeight)/2-100;--- its give total of top height 

 window.open("yourpageurl.php",---- this important to provide your page name to open with

'newWin',"chrome=yes, minimizable=no,width="+strwidth+",-- we can provide info for chrome flag

left="+leftStr+",top="+topStr

 fullscreen=yes, resizable=no, 

alwaysraised=yes, scrollbars=yes, 

menubar=no, toolbar=no, titlebar=no, statusbar=no, 

location=no, directories=no); }

As per the above logic, we can create a window as a new tab.  and can set data according to as per our needs.