You are reading the article Google Recommends Using Javascript “Responsibly” updated in December 2023 on the website Cattuongwedding.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 Google Recommends Using Javascript “Responsibly”
Google’s Martin Splitt, a webmaster trends analyst, recommends reducing reliance on JavaScript in order to provide the best experience for users.
In addition, “responsible” use of JavaScript can also help ensure that a site’s content is not lagging behind in Google’s search index.
These points were brought up during the latest SEO Mythbusting video which focuses on web performance.
Joined by Ada Rose Cannon of Samsung, Splitt discussed a number of topics about web performance as it relates to SEO.
The discussion naturally led to the topic of JavaScript, as overuse of JS can seriously drag down the performance of a website.
Here are some highlights from the discussion.
JavaScript sites may be lagging behindOveruse of JavaScript can be especially detrimental to sites that publish fresh content on a daily basis.
As a result of Google’s two-pass indexing process, fresh content on a JS-heavy site may not be indexed in search results for up to a week after it has been published.
When crawling a JS-heavy web page, Googlebot will first render the non-JS elements like HTML and CSS.
The page then gets put into a queue and Googlebot will render and index the rest of the content when more resources are available.
Use dynamic rendering to avoid a delay in indexingOne way to get around the problem of indexing lag, other than using hybrid rendering or server-side rendering, is to utilize dynamic rendering.
Dynamic rendering provides Googlebot with a static rendered version of a page, which will help it get indexed faster.
Rely mostly on HTML and CSS, if possibleWhen it comes to crawling, indexing, and overall user experience its best to rely primarily on HTML and CSS.
Splitt says HTML and CSS are more “resilient” than JavaScript because they degrade more gracefully.
For further information, see the full video below:
You're reading Google Recommends Using Javascript “Responsibly”
Practical Code Examples Using Javascript
Example#1: JavaScript Multiplication Table
Create a simple multiplication table asking the user the number of rows and columns he wants.
Solution:
var rows = prompt(“How many rows for your multiplication table?”); var cols = prompt(“How many columns for your multiplication table?”); rows = 10; cols = 10; createTable(rows, cols); function createTable(rows, cols) { var j=1; for(i=1;i<=rows;i++) { while(j<=cols) { j = j+1; } j = 1; } document.write(output); }
Example#2: JS Forms Example:Create a sample form program that collects the first name, last name, email, user id, password and confirms password from the user. All the inputs are mandatory and email address entered should be in correct format. Also, the values entered in the password and confirm password textboxes should be the same. After validating using JavaScript, In output display proper error messages in red color just next to the textbox where there is an error.
Solution with Source Code:
var divs = new Array(); divs[0] = “errFirst”; divs[1] = “errLast”; divs[2] = “errEmail”; divs[3] = “errUid”; divs[4] = “errPassword”; divs[5] = “errConfirm”; function validate() { var inputs = new Array(); inputs[0] = document.getElementById(‘first’).value; inputs[1] = document.getElementById(‘last’).value; inputs[2] = document.getElementById(’email’).value; inputs[3] = document.getElementById(‘uid’).value; inputs[4] = document.getElementById(‘password’).value; inputs[5] = document.getElementById(‘confirm’).value; var errors = new Array(); for (i in inputs) { var errMessage = errors[i]; var div = divs[i]; if (inputs[i] == “”) document.getElementById(div).innerHTML = errMessage; else if (i==2) { var atpos=inputs[i].indexOf(“@”); var dotpos=inputs[i].lastIndexOf(“.”); else document.getElementById(div).innerHTML = “OK!”; } else if (i==5) { var first = document.getElementById(‘password’).value; var second = document.getElementById(‘confirm’).value; if (second != first) else document.getElementById(div).innerHTML = “OK!”; } else document.getElementById(div).innerHTML = “OK!”; } } function finalValidate() { var count = 0; for(i=0;i<6;i++) { var div = divs[i]; if(document.getElementById(div).innerHTML == “OK!”) count = count + 1; } if(count == 6) document.getElementById(“errFinal”).innerHTML = “All the data you entered is correct!!!”; } Example#3: POPUP Message using Event:
Display a simple message “Welcome!!!” on your demo webpage and when the user hovers over the message, a popup should be displayed with a message “Welcome to my WebPage!!!”.
Solution:
function trigger()
{
document.getElementById(“hover”).addEventListener(“mouseover”, popup);
function popup()
{
alert(“Welcome to my WebPage!!!”);
}
}
p
{
font-size:50px;
position: fixed;
left: 550px;
top: 300px;
}
Google Travel Updates How It Recommends Flights And Hotels
Google has updated it’s travel portal to include pandemic relevant information. Now Google’s travel portal is showing Travel Trends that reveal additional information about flights and hotels, changing how decision making is done.
Travel TrendsThe travel trends shows consumers multiple kinds of information that takes into account the Covid-19 epidemic.
The changes improve on pandemic related features already in search and Google Travel.
Google already provides Covid-19 related warnings about destinations in the regular search:
The new changes affect the Google Travel portal and give additional information on a more granular level.
For example, Google Travel will show useful information like hotel and flight availability for each destination.
Hotel and Flight Availability InformationAccording to Google:
“In the next week, you’ll see the percentage of open hotels with availability and flights operating at the city or county level based on Google Flights and Hotels data from the previous week.
When you visit chúng tôi and tap on a trip you’re planning, or search for hotels and things to do, you’ll now see trendlines for hotel and flight availability. Links to additional local resources, including the number of COVID-19 cases, are provided as well.”
The context of the user interface is hotels. So the interface starts out showing hotel related information.
Screenshot Of Hotel and Flight Availability Travel Advisory WarningAs you can see in the screenshot below, Google says that Las Vegas is trending upward.
Screenshot of Google Warning Shows Las Vegas is Trending for Covid-19 Free Cancellation FilterThe Google travel portal has also added a filter that when toggled shows only hotels that offer free cancellation.
Google added this filter to help travelers who might change their mind because of Covid-19 considerations.
According to Google:
“Due to the uncertainty around COVID-19, people often want flexibility when making travel plans. Many hotels and vacation rentals now offer free cancellation to give travelers more confidence when planning trips. Search for a hotel, and later this month a vacation rental, on chúng tôi and filter to see only rooms or properties with free cancellation policies.”
A Lesson in Being Useful Google Travel and Covid-19 RelevanceGoogle’s travel portal is an example of how to be useful and relevant to travelers.
Whatever their reasoning, chúng tôi does not provide the information itself.
Google takes a different approach by providing the information that is useful to users. By doing that, Google keeps users on their site and they also makes users happy, thereby building loyalty.
Google has failed at a lot of things, like building a social network or creating a competitor to YouTube.
Those failures demonstrate that it’s not enough for Google to “favor” its own properties in order to beat their competitors. It’s not that simple, it takes more than that.
If other companies focused more on keeping their users informed and happy, the loyalty that creates would keep them from running to Google.
Being relevant and keeping users happy is what keeps users returning to Google, it’s what they do best.
Google’s new travel portal is Google doing what they do best, organizing the world’s information and making it useful.
CitationsOfficial Google Announcement
Make Travel Decisions with Confidence
How To Create A Dropdown List Using Javascript?
We will learn to create a dropdown list using HTML and JavaScript below. Before starting with the article, let’s understand the dropdown list and why we need to use it.
The dropdown list gives multiple choices to users and allows them to select one value from all options. However, we can do the same thing using multiple radio buttons, but what if we have hundreds of choices? Then we can use the dropdown menu.
Syntaxfunction selectOption() { let selectedValue = dropdown.options[dropdown.selectedIndex].text; } Example
In the example below, we have created the dropdown menu for car brands. Also, we have written the JavaScript code to get the selected value from the dropdown. The ‘onchange’ event will trigger whenever the user selects new values and invoke the selectOption() function.
Also, we have given some CSS styles to the default dropdown menu. Furthermore, we hide the dropdown menu’s arrow to improve it. In CSS, users can see how they can customize the default dropdown.
let output = document.getElementById(‘output’); function selectOption() { let dropdown = document.getElementById(‘dropdown’); let selectedIndex = dropdown.selectedIndex; let selectedValue = dropdown.options[selectedIndex].text; output.innerHTML = “The selected value is ” + selectedValue; }
We can use normal HTML, CSS, and JavaScript to create a dropdown menu from scratch. We can use HTML to make dropdowns, CSS to style them properly, and JavaScript to add behavior.
StepsUsers can follow the steps below to create a dropdown menu using HTML, CSS, and JavaScript.
Step 1 − Create a div element for the dropdown title, and style it using CSS. We have created the div element with the ‘menu-dropdwon’ class.
Step 2 − Create a div element with the ‘dropdown-list’ class to add dropdown options.
Step 4 − Now, use JavaScript to add the behavior to our dropdown.
Step 6 − In the openDropdown() function, access the div element with the class name ‘dropdown-list’ and show if it’s hidden or hides it if it is visible using the display property.
Example.menu-dropdown { width: 10rem; height: 1.8rem; font-size: 1.5rem; background-color: aqua; color: black; border: 2px solid yellow; border-radius: 10px; padding: 2px 5px; text-align: center; justify-content: center; cursor: pointer; } .dropdown-list { display: none; z-index: 10; background-color: green; color: pink; font-size: 1.2rem; width: 10.5rem; border-radius: 10px; margin-top: 0rem; cursor: pointer; } .dropdown-list p { padding: 3px 10px; } .dropdown-list p:hover { background-color: blue; } Choose Value let output = document.getElementById(‘output’); let dropdownList = document.getElementById(“list”); dropdownList.style.display = “none”; function openDropdown() { if (dropdownList.style.display != “none”) { dropdownList.style.display = “none”; } else { dropdownList.style.display = “block”; } } const p_elements = document.getElementsByTagName(“p”); const totalP = p_elements.length; for (let i = 0; i < totalP; i++) { const option = p_elements[i]; output.innerHTML = “The selected option is ” + option.innerHTML; dropdownList.style.display = “none”; }) }
How To Configure Mouse Wheel Speed Across Browsers Using Javascript?
We can use JavaScript to change the behavior of the web page. Every browser has a default scrolling speed when users scroll using the mouse wheel. However, we can control it using JavaScript.
We can also use the mouse wheel to zoom in and out of web pages. In such cases, we require to reduce the mouse wheel speed. Furthermore, developers sometimes require a scrolling speed limit, such as on website rules, so that users can read it properly.
In this tutorial, we will learn different ways to control the mouse’s wheel speed.
SyntaxUsers can follow the syntax below to use the ‘wheel’ event to configure mouse wheel speed across browsers using JavaScript.
let deltaY = event.deltaY; content.scrollTop += deltaY / n; });
We take the current scrolling speed using the deltaY property in the above syntax. After that, we divide the current scrolling speed by n to change the scrolling speed. Developers should increase the value of n to decrease the scrolling speed and decrease the value of n to increase the scrolling speed.
Example 1 (Using the Wheel Event)In the example below, we have created the content div element and added the text content. In CSS, we have set the dimensions of the div element and set the overflow scroll to make the div scrollable.
In JavaScript, we used the addEventListner() method to fire the ‘wheel’ event and get the current scrolling speed. After that, we have decreased the scrolling speed by 50 times. In the output, users can try to scroll through the div element and observe the slow scrolling speed.
#content { height: 300px; width: 300px; overflow-y: scroll; padding: 10px; font-size: 20px; font-family: Arial, sans-serif; background-color: pink; } const content = document.getElementById(“content”); event.preventDefault(); let deltaY = event.deltaY;
let speed = deltaY / 50;
content.scrollTop += speed; }); Example 2 (Using the Mousewheel Event For Chrome Browser)
In the example below, we have used the ‘mousewheel’ event. When users use the mouse wheel to scroll the web page in the Chrome browser, it fires the ‘mousewheel’ event.
Here, we have multiplied the current scrolling speed by 0.03 to decrease the scrolling speed by 97%. However, it is very similar to the ‘wheel’ event.
#content { height: 200px; width: 200px; overflow-y: scroll; padding: 10px; font-size: 60px; font-family: Arial, sans-serif; background-color: aqua; } const content = document.getElementById(“content”); event.preventDefault(); const deltaY = event.deltaY; content.scrollTop += deltaY * 0.03; }); Example 3 (Using the DomMouseScroll Event for Firefox Browser)
In the example below, we have used the ‘DomMouseScroll’ event. The Firefox browser only supports it but not other browsers such as Chrome, opera, etc.
We used the ‘detail’ property of the event to get the current scrolling speed and multiplied it by 0.5 to reduce the scrolling speed by half. Users can open the below web page in the Firefox browser the observe the change in the scrolling speed of the div element.
#content { height: 200px; width: 200px; overflow-y: scroll; padding: 10px; font-size: 40px; background-color: green; } const content = document.getElementById(“content”); event.preventDefault(); const deltaY = event.detail; content.scrollTop += deltaY * 0.5; }); Example 4 (Customizing the Mouse Wheel Speed From the Web Page)
In the example below, we allow users to change the mouse wheel scrolling speed from the web browser. We have created the range slider that takes the input between 1 and 50. Users can change the value of the range slider.
After that, whenever users scroll the div element, JavaScript sets the scrolling speed according to the selected value in the range slider.
#content { height: 200px; width: 200px; overflow-y: scroll; padding: 10px; font-size: 20px; font-family: Arial, sans-serif; background-color: aqua; } const content = document.getElementById(“content”); event.preventDefault(); let value = document.getElementById(“myRange”).value; let deltaY = event.deltaY; deltaY = value; } else if (deltaY < 0) { deltaY = -value; } content.scrollTop += deltaY; });
Users learned to control the mouse wheel scrolling speed across browsers using JavaScript. This tutorial used three events to configure the mouse wheel speed. The first event is the ‘wheel’ event which is compatible with all browsers. The second event is ‘mousewheel’, compatible with all browsers except Firefox. The third event is ‘DOMMouseScroll’, which is only compatible with Firefox.
How To Check Input File Is Empty Or Not Using Javascript Jquery
In JavaScript, while working with the form elements, we need to validate the input fields and form elements when a user enters the value. In this tutorial, we will work with the file input.
We will also learn to validate the file input. Sometimes, we may be required to check if the file is selected in the input field, then only enable the submit button; otherwise, disable the submit button. So, it will not allow users to submit the form or file without selecting it.
Validate the file input using JavaScriptIn JavaScript, we can access the file input using the id, name, tag name, or class name. After that, we can check whether the file input field contains any file value. We can add the event listener to the file input for the change event. So, whenever the value of the file input changes, the event listener is invoked, and we can check whether the file input contains any file.
SyntaxUsers can follow the syntax below to use JavaScript to check if the file input is empty or not.
fileInput.addEventListener("change", function () { if (fileInput.files.length == 0) { } else { } });In the above syntax, we have added the event listener for the change event in the file input. Also, we have passed the function expression as a callback function, which checks whether any file is selected.
The file input contains the array of the file, and we can check if the length of the array is zero if any file is not uploaded to the file input.
Examplelet fileInput = document.getElementById(“fileInput”); fileInput.addEventListener(“change”, function () {
if (fileInput.files.length == 0) { } else { } });
Validate the file input using jQueryAs we can access the file input in JavaScript, we can also access it in jQuery using various methods. After that, we can check the length of the ‘files’ attribute of the file input to ensure whether the file input is empty or not.
SyntaxUsers can follow the syntax below to use jQuery to check if the file input contains any file
let files = $("#fileInput")[0].files.length; if (files != 0) { } else { }In the above syntax, we have selected the file input by its id. The ‘#’ represents the id.
Examplelet output = document.getElementById(“output”); function validateFileInput() { let files = $(“#fileInput”)[0].files.length; if (files != 0) { } else { } }
Users learned to validate the file input using JavaScript or JQuery. In both examples, we have accessed the file input using the id. However, users can access the file input using either class name, tag name, or name and check the length of the ‘files’ attribute to ensure whether the file input is empty or any file is selected.
Update the detailed information about Google Recommends Using Javascript “Responsibly” on the Cattuongwedding.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!