Ejercicios Resueltos de JavaScript
Ejercicios Resueltos de JavaScript
To open a new browser window with a specified configuration in JavaScript, the 'window.open()' method is used along with parameters that specify the URL, a window name, and a features string. The features string can include options like 'location', 'status', 'width', and 'height' to customize the appearance and functionality of the new window. For instance, 'window.open('http://www.luisbonilla.com','','location=yes,width=300,height=400')' opens a window with specified dimensions and a location bar .
JavaScript can print a webpage using the 'window.print()' function. This function can be called directly from a button or a hyperlink. For the button method, an 'input' element of type 'submit' can include 'onclick="javascript:window.print()"'. For the link method, an 'a' element can link to 'javascript:window.print()'. Both methods trigger the browser's printing capabilities when clicked .
To display odd numbers from 1 to 99, start by initializing the variable 'a' to 1. Use a for loop to iterate over the range with 'for (a=1; a<=100; a++) { a++; }'. In each iteration, document.write is used to output the current value of 'a', which would represent all odd numbers. This method increments 'a' by 2, effectively skipping even numbers .
A multiplication table can be created by first using 'prompt()' to ask the user for a number. Then, a 'for' loop runs with a counter variable from 0 to 10. Within the loop, the document.write function is used to display the multiplication of the user-provided number with the counter variable, formatted as 'number * counter = result'. For example, if the user inputs '2', the script displays '2 * 0 = 0', up to '2 * 10 = 20' .
Disabling right-click with JavaScript can prevent certain user actions, like copying content, but it also has security implications. It may disrupt standard user navigation and lack effectiveness as a security measure, given that it can be bypassed using browser settings or scripts. Over-relying on such measures without server-side support poses a false sense of security .
JavaScript can display even numbers from 0 to 100 using a for loop by incrementing the variable 'a' by 2 each iteration. The essential code structure involves initializing a variable 'a' to 0, using a for loop: 'for (a=0; a<=100; a++)', and incrementing 'a' by 2 within the loop, ensuring even numbers are shown. The document.write function is then used to display the current value of 'a' at each iteration .
JavaScript scripts customize new windows by specifying parameters in 'window.open()'. This method takes a URL, a window name, and a string indicating features like 'width', 'height', 'location', and 'status'. Developers can tailor these features to match the visual requirements of the application, making new windows better integrated in terms of size and tool availability .
JavaScript uses the 'document.onmousedown' event to manage mouse interactions. Assigning a function to this event enables execution whenever a mouse button is pressed, such as preventing right-click menus. This approach allows for real-time monitoring but can degrade user experience if overused, and is vulnerable to being overridden by future events unless properly designed .
The 'javascript:' prefix in an 'href' makes the link behave as an executable script rather than a navigation instruction. It's vital for ensuring that actions like triggering 'window.print()' execute immediately when the hyperlink is clicked, without navigating to a different URL. It thereby integrates functionalities like printing directly into clickable elements .
A JavaScript function can prevent the right-click context menu by intercepting the 'onmousedown' event, and specifically checking if the right mouse button (event.button==2) is pressed. If so, a custom alert message can be triggered. This is achieved by assigning the function 'controlraton' to 'document.onmousedown', ensuring that any mouse click will trigger this function .