JavaScript

How do I redirect to another webpage?

dev dcos

If you want to simulate someone clicking on a link, use location.href If you want to simulate an HTTP redirect, use location.replace For example: // similar behavior as an HTTP redirect window.location.replace(“http://stackoverflow.com”); // similar behavior as clicking on a link window.location.href = “http://stackoverflow.com”; Sources:  stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage

How to scroll to specific element using jQuery ?

Scroll to an element with jQuery   <script type=”text/javascript”> jQuery(“.downarrow-go-down”).click(function() { jQuery([document.documentElement, document.body]).animate({ scrollTop: jQuery(“#pricing”).offset().top }, 2000); }); </script> Source: https://stackoverflow.com/questions/6677035/scroll-to-an-element-with-jquery

format a date in JavaScript?

To format a date use the following code –  <script type=”text/javascript”> var options = { weekday: ‘long’, year: ‘numeric’, month: ‘long’, day: ‘numeric’ }; var today = new Date(); console.log(today.toLocaleDateString(“en-US”)); // 9/17/2016 console.log(today.toLocaleDateString(“en-US”, options)); // Saturday, September 17, 2016 console.log(today.toLocaleDateString(“hi-IN”, options)); // शनिवार, 17 सितंबर 2016 </script>

JavaScript For Loop

<h2>JavaScript For Loop</h2> <p id=”demo”></p> <script> const cars = [“BMW”, “Volvo”, “Saab”, “Ford”, “Fiat”, “Audi”]; let text = “”; for (let i = 0; i < cars.length; i++) { text += cars[i] + “<br>”; } document.getElementById(“demo”).innerHTML = text; </script>

html sortable table with Javascript

Click the button to sort the table alphabetically, by name: Sort Name Country Berglunds snabbkop Sweden North/South UK Alfreds Futterkiste Germany Koniglich Essen Germany Magazzini Alimentari Riuniti Italy Paris specialites France Island Trading UK Laughing Bacchus Winecellars Canada Codes  <p>Click the button to sort the table alphabetically, by name:</p> <p><button onclick=”sortTable()”>Sort</button></p> <table id=”myTable”> <tr> <th>Name</th> […]

How can I prevent href redirect onclick with JavaScript

prevent href redirect onclick with JavaScript Use – d.preventDefault();   jQuery(document).on(“click”,”.seestock”,function(e) { e.preventDefault(); //jQuery(‘> span .stockstatus’, this).html(‘Save’); var qty = jQuery(this).data(‘qty’); jQuery(this).html(qty); //alert(‘hello’); });

Randomize a List – Display HTML List Items Randomly with Javascript

To randomize a list in HTML with Javascript use the following codes HTML CODE <div class=”container”> <h3>Shuffle List Randomly when page reload.</h3> <ul id=”grid” class=”list-inline”> <li>Runner: Alto’s Odyssey</li> <li>Chess: Really Bad Chess</li> <li>Puzzle: Mazes &amp; More</li> <li>Platform: Once Upon a Tower</li> <li>Arcade: Jungle Marble Blast</li> <li>Brainy: Quizoid</li> <li>Action: Tank Hero: Laser Wars</li> <li>Puzzle: Sudoku</li> <li>Shooter: […]

Generating PDF files with JavaScript

Generate PDF with JavaScript // Default export is a5 paper, portrait, using milimeters for units var doc = new jsPDF() doc.text(‘Hello world!’, 10, 10) doc.save(‘a5.pdf’) <script src=”https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.debug.js”></script> Source: https://stackoverflow.com/questions/742271/generating-pdf-files-with-javascript

jQuery AJAX submit form | How To Submit AJAX Forms with JQuery

Submit AJAX Forms with JQuery // this is the id of the form $(“#idForm”).submit(function(e) { e.preventDefault(); // avoid to execute the actual submit of the form. var form = $(this); var url = form.attr(‘action’); $.ajax({ type: “POST”, url: url, data: form.serialize(), // serializes the form’s elements. success: function(data) { alert(data); // show response from the […]

Creating a Notification with JavaScript and blink it every 1.5 second

Create blinking notification with JavaScript Style .notification { position: fixed; z-index: 1000; padding: 5px; border: 1px solid black; font-size: 20px; background: white; text-align: center; } .welcome { background: #b80000; color: yellow; } JS – function showNotification({top=0,right=0,background=”#ededed”,color=”blue”,html=””}){ let notification = document.createElement(‘div’); notification.className = “notification”; notification.innerHTML = html; notification.style.top = top+”px”; notification.style.color = color; notification.style.right = right+”px”; […]

How To Create a Scroll Back To Top Button with JavaScript

How To Create a Scroll Back To Top Button with JavaScript window.onscroll = function(){scrollFunction()}; let scroll = document.getElementById(‘scroll-top-btn’); if(scroll){ scroll.style.display = “none”; } function scrollFunction(){ if(document.body.scrollTop > 180|| document.documentElement.scrollTop > 180){ //document.getElementById(‘menu’).style.cssText = “position: fixed; width: 100%; z-index: 9; background: #fff; top: 0;”; if(document.getElementById(‘menu’).classList.contains(‘scroll-menu-active’)==false){ //document.getElementById(‘menu’).classList.add(‘scroll-menu-active’); document.getElementById(‘menu’).className += ” “+”scroll-menu-active”; } }else{ document.getElementById(‘menu’).classList.remove(‘scroll-menu-active’) } if(document.body.scrollTop > […]

How to replace all occurrences of a string in JavaScript

Replace all occurrences of a string in JavaScript <script type=”text/javascript”> function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[]\]/g, ‘\$&’); // $& means the whole matched string } function replaceAll(str, find, replace) { return str.replace(new RegExp(escapeRegExp(find), ‘g’), replace); } </script>