I want to know that how can i create a JavaScript code that must open a new page at click anywhere at website. currently i am using onload script.
<script type="text/javascript">
function open_on_entrance(url,name)
{
window.open('http://realestatepakistan.pk','Real Estate Pakistan')
}
</script>
<body onload="open_on_entrance()"></body
but onload is not effective as it opens very low number of pages. A must must open page script needed fro my web http://pkr.com.pk/.
This code will open the popup when you click anywhere on website.
<script type="text/javascript">
var shouldOpenWindow = true;
function open_on_click(url,name) {
if (shouldOpenWindow) {
window.open('http://realestatepakistan.pk','Real Estate Pakistan');
// if you want that only on first click the popup must be opened, and not on any subsequent clicks, then do this
shouldOpenWindow = !shouldOpenWindow;
}
}
</script>
<body onclick="open_on_click()"></body>
Place this code in a script tag right before your closing </body> tag that way it will load after the dom has loaded. You will notice that I removed your parameters from your function because you were not using them inside of your script. Make sure you change the values of window.open to the values you need for your script.
JavaScript:(live example)
function open_on_entrance() {
window.open('http://bytewarestudios.com','Welcome to Byteware Studios');
}
document.addEventListener("click", function() {
open_on_entrance();
});
If you decide to the add the parameters back in make sure you supply them to your function when you call it.
Example Using Parameters:
function open_on_entrance(url,name) {
window.open(url,name);
}
document.addEventListener("click", function() {
var url = 'http://bytewarestudios.com';
var name = 'Welcome to Byteware Studios';
open_on_entrance(url,name);
});
Related
Currently assigned the task of creating a website which uses python to visualize a network. With this, after clicking a button, the python script will run off of specific parameters the user sets. To avoid a huge load request, we only want the user to click the button once, and not be able to run the script multiple times. The idea we came up with was having the button be clicked once, and then after the one request, become disabled(until they refresh the page, although I know they can just run it again, but we will discuss that problem once we get there). We are currently using an anchor tag to call upon the python script using PHP as well. I believe that since we are using an anchor tag, we may have to change the href attribute of the anchor tag so that it just doesn't run the python script numerous times, making the button useless even if its clickable.
We've tried using JavaScript functions:
let toggleVisual = false;
let hideVisual = function() {
let visual = document.getElementsById('visual');
if (toggleVisual = false) {
visual.disabled = true;
toggleVisual = true;
}
}
and more JavaScript:
document.getElementById('form-button').onclick = function () {
this.disabled = true;
}
and even some HTML:
<button id="form-button" class="visualize-btn">Visualize</button>
PHP code calling python script:
<?php
if (isset($_GET["run"])) {
$output = shell_exec('sh test.sh');
echo "<pre>$output</pre> works";
}
?>
I expect that when I click the button to run the script, the button (or anchor tag) will become disabled, ensuring that the python script won't run multiple times.
Welcome to StackOverflow! You were close.. Check out the onclick() event, you'll probably like it. A simple code based on your first attempt would be something like:
<button id="form-button" class="visualize-btn" onclick="doSomething();">Visualize</button>
<script type="text/javascript">
var wasClicked = false;
function doSomething() {
if(wasClicked) { return; }
wasClicked = true;
// Your code here
alert('Hello world!');
}
</script>
Best of luck with your website!
I create a web app that looks like this:
When i click the run model, i want that the form :"Dashbord", will open.
The JS code:
<script>
window.onload = function () {
function newDoc() {
window.location.href("#http://127.0.0.1:8100/#dashboard");
}
}
</script>
When The "Run Model" button onClick activate the function: newDoc().
The problem is: that in my URL path it is written: http://127.0.0.1:8100/#dashboard
but the 'Dashboard' form is not logged. it stays in the same page.
What should i do?
window.location.href is not a method, it's a property.
Try assigning it instead (also note, I removed the leading # character)...
window.location.href = "http://127.0.0.1:8100/#dashboard";
You also need to move your function outside of the window.onload event...
window.onload = function () {
}
function newDoc() {
window.location.href = "http://127.0.0.1:8100/#dashboard";
}
The onload event handler is generally only needed when you're dealing with specific elements on the page that won't be available until the page has finished loading.
By putting the newDoc within the onload event, you were effectively hiding it from being used directly by other events.
this webpage is built for firefox, but it has to display a link which is to be opened in IE. So I am trying to use a code I found on net. but it is not working. what am I missing?
javascript code:
<script type="text/javascript">
$(document).ready(function(){
function myFunction()
{
alert("opening now....");
var localFile = Components.classes["#mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
var process = Components.classes["#mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
var url = content.document.location.href;
var args = ["-new-tab", url];
localFile.initWithPath("C:\\Program Files \(x86\)\\Internet Explorer\\iexplore.exe");
process.init(localFile);
process.run(false, args, args.length);
}
});
HTML Code:
<p>Click the link top open in IE</p>
open in IE
When I click on link, it opens the google page: in same window, in same borser, in same tab.Pl advice.
thank you.
myFunction is not defined in the global scope. Adding it to the dcoument ready callback guarantuees this.
You could move myFunction outside of the document ready callback. However:
In this case I would attach a click handler using jquery as follows
$(function() {
$("a").click(function() {
// do stuff
});
})
Note this selects all anchors and applies the same event handler. Ideally you would use a selector.
$("#myGoogleLink").click(function() {
})
Add the id myGoogleLink to your html anchor
Here is the issue at hand:
The overall development is being done using Ruby on rails; however, the views consist of mostly html and jQuery. Currently, I have it set up so that when a user types into a text field, they can press a small "suggest" button beneath it which opens up a Fancybox where there is a list of useful Search terms, provided by the Google Suggest API. This is all set up and working. Now I want to take this to the next step, where, from inside of the Fancybox, the users can click on one of the suggestions and it will replace the initially typed in phrase in the parent window. Sadly I am not adept at using AJAX yet so I am trying to do this via javascript. This is what I have thus far:
In the parent window:
<script type="text/javascript">
$(document).ready(function() {
var $_returnvalue = false;
$('.suggest_link').fancybox({
onClosed: function(){
alert($_returnvalue);
if ($_returnvalue != false)
{
// I will be setting the textbox value here.
}
}
});
</script>
In the partial view rendered inside of the fancybox:
<script type="text/javascript">
$(document).ready(function() {
var $_fancyvalue = false;
$(".suggestion").click(function(){
alert(parent.$_returnvalue);
parent.$_returnvalue = $(this).text();
$.fancybox.close();
});
});
</script>
Sorry if there is anything strange with this post. This is my first time asking a question here.
Define var $_returnvalue in the global scope in the parent window. Try this it will work fine.
var $_returnvalue = false;
$(document).ready(function() {
$('.suggest_link').fancybox({
onClosed: function(){
alert($_returnvalue);
if ($_returnvalue != false)
{
// I will be setting the textbox value here.
}
}
});
So I'm trying to redirect users from html links and element id tags, to other pages with javascript. I've figured out how to do one singular redirect but having trouble writing the code for multiple links heres what I have so far:
HTML:
<head>
<script type = "text/javascript" src="script2.js"></script>
</head>
<body>
NickName
Salestax
W 3 Schools
</body>
</html>
My external script so far for just one link:
window.onload = initAll;
function initAll() {
document.getElementById("redirect").onclick = initRedirect;
}
function initRedirect() {
confirm("Go to Salestax page?");
window.location="salestax.html";
return false;
{
Do I just crank out more functions and change the location value, getElementById value and the onclick value?
A nice thing about onclick events is that if you return false it'll stop the browser from going to the link defined in the HREF. If you return true it'll keep going through with the navigation. You don't need to worry about the window.location in your function if you use this method, which will simplify things a lot. So you can just do:
Salestax
I'll prompt them if they want to continue. They click yes it'll keep going as if they clicked the link normally, they click no it'll stop them from navigating away. This way you don't have to duplicate the link's HREF in both your HTML and javascript.
You could still dynamically bind this, but if you're doing it by ID I don't really see any advantage vs just defining the onclick in the HTML.
First off, unless you're trying to prevent a user from losing changes that they've made on the current page, it's not clear why you would want to create this functionality. But at any rate, here's a standard/basic approach:
window.onload = function() {
document.getElementById("redirect").onclick = redirectConfirmation("Go to Salestax page?");
document.getElementById("redirect1").onclick = redirectConfirmation("Go to Nickname?");
};
redirectConfirmation = function(msg, urlOverride) {
return function() {
if ( confirm(msg) ) {
window.location = urlOverride || this.href || "#"
}
return false;
};
};
redirectConfirmation optionally takes a second parameter which can be used to explicitly set the url that the page is redirected to; otherwise, it will default to the URL specified by the href attribute of the anchor tag being acted upon (and if all else fails, it will fail gracefully with "#").
If you're using a common library, like jQuery, you can simplify your event registration as follows:
$(function() {
$("#redirect").click( redirectConfirmation("Go to Salestax page?") );
$("#redirect1").click( redirectConfirmation("Go to Nickname?") );
});
A far better approach would be to do something like the below - that way the logic for redirecting the user stays reasonably close to the link, and so people looking at the source wont become incredibly confused when the page takes people elsewhere.
Some link
External script:
function initRedirect(message, url)
{
confirm(message);
window.location = url;
return false;
}