Multiple lines in onClick? - javascript

I have a link and I need it to do two thing when it's clicked. First, it tracks the h it using the google tracking code, and the next is that it opens a new window.
I tried:
onclick="javascript: pageTracker._trackPageview('/Ads/MMA_Front_Page.com'); window.open(this.href, '_blank'); return false;"
But it didn't work, it didn't open a new window.
Did I do something wrong with the code or do I need to create a javascript method and call that?
Thanks
Edit: I am trying with functions, but it wont work. Can you help me please?
Here is the function:
<script type="text/javascript">
function openAd(adType) {
pageTracker._trackPageview(adType);
window.open(this.href, '_blank');
return false;
}
</script>
And the part where i call it:
onclick="openAd('/Ads/MMA_Front_Page.com')"
It seems only the first part is working "pageTracker._trackPageview(adType);" and the rest is being ignored.

You should really use functions inside your <script> tag but, try without the javascript:... like:
onclick="pageTracker._trackPageview('/Ads/MMA_Front_Page.com'); window.open(this.href, '_blank'); return false;"

onclick="do_something();"
<script type="text/javascript">
function do_something(){
pageTracker._trackPageview('/Ads/MMA_Front_Page.com');
window.open(this.href, '_blank');
return false;
}
</script>
You have included answer in your question. It will be better to create a function for multiple statement.

Related

The onclick function not work in safari

Please help me on this how can i use hash function to go to another tab or id without location.reload();.
it should be needed to go to the download tab or id without needing to refresh the page.
here is my code
<script>
function Download() {
window.location.hash = "#Download";
location.reload();
}
</script>
Do you actually need to use JavaScript for this? What about just using #href attributes to go to another <a> link on the page?
Something like this:
<a id='first' href='#second'>Go to Second Link.</a>
<a id='second' href='#first'>Return to First Link.</a>
See this fiddle for example: https://jsfiddle.net/3axqpr1x/1/
you can try this one;
<script>
function Download() {
window.location.hash = "#Download";
location.reload();
}
</script>
Please refer this page Javascript reload the page with hash value

Change an image on click with Javascript, but still following the link

I have the following html
<a href="email.html" target="_blank" ><img src="../img/mailto.gif" alt="Send email" id='email'/></a>
Clicking the image should open a new window and you should now if that image has been clicked on before, simply because it would change when you click on it.
This is also included in a table created with PHP from a MySQL table (basically, this means I will get a new image in every row and I can only change their ID's globally, not one by one..)
After adding this jquery code the link stopped working. Which means that, when I click on the image it changes to a different one (.../img/mailto_onclick.gif) and that's fine, but the email.html page doesnt open in a new tab like it used to...
<script language="javascript">
$(document).ready(function() {
$('#email').click(function(){
$(this).attr('src',"../img/mailto_onclick.gif");
return false;
});
});
</script>
Any thought's on how to get this working?
Sorry if it's some basic or obvious stuff
Remove return false as it will prevent the default behavior for click() because <img> is wrapped inside <a></a>.
$('#email').click(function(){
$(this).attr('src',"../img/mailto_onclick.gif");
// return false; // remove
});
Try this:
<script language="javascript">
$(document).ready(function() {
$('#email').click(function(){
$(this).attr('src',"../img/mailto_onclick.gif");
//return false;
});
});
</script>

Creating link with javascript

I am trying to create a link in JS that moves the person the page from where he has come from. Here is the code.
<script language="javascript">
function Jump()
{
document.href=document.referrer;
}
</script>
Here is the html,
Skip and Continue
Now when the user clicks on the link, nothing happens. Please guide me where I am doing wrong. Thanks
how about using the below code to move back
history.back();
Many browsers will not use document.referrer for privacy reasons, especially if the referrer is from another domain.
Instead, try onclick="history.go(-1)" instead of your Jump() function.
It is better to bind a click listener than use onclick.
Try changing it to this:
<a id="myLink" href="#">Skip and Continue</a>
And Javascript:
<script type="text/javascript">
document.getElementById('myLink').addEventListener('click', function(e) {
e.preventDefault();
document.location.href=document.referrer; //actually better as "history.back()"
}
</script>
It is not document.href but window.location.href...
<script>
function Jump(){
if(document.referrer)location.href=document.referrer;
else history.back();
}
</script>
(If you used the jump to get to the current page, there is no referrer.)

html link, href assignment from a Javascript function

I have a simple Javascript function which builds a Url that I want to provide a link to.
However, I can't seem to get the anchor tag working with it. How do I assign the href of the anchor tag the results of the Javascript function?
Neither one of these work correctly:
Click here
Click here
This is what I want to accomplish.
<script type="text/javascript">
function getUrl()
{
return "http://www.google.com";
}
</script>
Click here
-- update --
If I wanted to incorporate user278064s' comments, i would change the above into:
<script type="text/javascript">
function getUrl()
{
return "http://www.google.com";
}
</script>
Click here
None of the above solutions worked for me. A good way would be to create a new link in the function.
function fetchURL() {
var title = "Download";
var URL = title.link("https://www.google.com");
document.getElementById("dynamicButton").innerHTML = URL;
}
<body onload="fetchURL()">
<div id="dynamicButton">
//empty
</div>
<a onclick="getUrl();" href="#">Click here</a>
Click Here
Give the link an id:
<a id="specialLink">Click Here</a>
Later on, set its href from JavaScript:
document.getElementById('specialLink').href = getUrl();
(You might want to include a placeholder href in the link which people with JavaScript disabled will see.)
function getUrl(that) {
return "www.whateveryouwant.com";
}
// Point the a.href attribute at your url.
var a = document.getElementsByTagName('a')[0];
a.href = getUrl();
UPDATE
I assume that you want to use the getUrl() method to set the href attribute, because probably the pointed url is not static (so it could change at any moment e point to the getUrl() returned url.)
Anyway, you could assign the href attribute, when i.e. the user click on the link, in this way.
function changeHref(aElem) {
aElem.href = getUrl();
}
Following the complete code:
click me!
<script>
function getUrl(that) {
return "www.whateveryouwant.com";
}
function changeHref(aElem) {
aElem.href = getUrl();
}
</script>
One other thing. You should avoid the use of javascript: pseudo-protocol.
This fragment will explain you why:
A pseudo-protocol is a nonstandard take on this idea.
The javascript: pseudo-protocol is supposed to be used to invoke JavaScript from within a link.
Here’s how the javascript: pseudo-protocol would be used to call the popUp function:
Example
This will work just fine in browsers that understand the javascript: pseudo-protocol. Older browsers, however, will attempt to follow the link and fail. Even in browsers that understand the pseudoprotocol,
the link becomes useless if JavaScript has been disabled.
In short, using the javascript: pseudo-protocol is usually a very bad way of referencing JavaScript
from within your markup.
DOM Scripting: Web Design with JavaScript and the Document Object Model: Second Edition
The following worked for me
<script type="text/javascript">
$(function() {
document.getElementById("aTag").href = getUrl();
});
</script>
<a id="aTag">Click Here</a>

Onclick in <a> cause reloading of the page insted of executing JS code

My URL: http://www.example.com/?product=3&url=XYZ
My code:
link
After click, page is reloading, instead of executing foo() function.
JS is enabled. Body of foo():
function foo() { alert("sss"); }
Probably, this problem is caused by URL of my site. XYZ parameter is a url of a website but with something like "%C5%82%C3%B3" instead of special characters (something like after using htmlspecialchars()).
What is interesting, after click the page is reloaded with the "normal" URL, something like: http://www.example.com/?product=3&url=http://www.example.com (WITH special characters like " / ").
What can I do to resolve this problem?
EDIT
Above code works fine. Thank you for your time.
Yeah, there's definitely something else going on here. Here's a minimal example that works just fine:
<html>
<body>
link
<script type="text/javascript">
function foo() { alert("hi"); }
</script>
</body>
</html>
Assigning onclick inline is not a good practice, and you should be doing something like
<a id="someId" /* ... */ >
// ...
<script type="text/javascript">
function foo() { alert("hi"); return false; }
document.getElementById("someId").onclick = foo;
</script>
but in any case, the most likely culprit is that your script has a syntax error somewhere and is not loading at all. You can verify this by setting onclick="return false". If that doesn't work, it's likely you have some other event handler that's being triggered. But because the above -- all we know of your code -- works, it's unlikely anyone here can diagnose what the problem is without more information.
Edit
Nevermind this, I fully assumed your code wasn't working and the below did. But I guess the code you posted was already correct.
On a seperate note, you should avoid using attributes like onClick, create event handlers instead (I suggest looking at jQuery).
i tried the same code in my systm.the code is workin and the alert box is visible.
My code:
<a href="#" onclick="foo();return false;">link/a>
and javascript is
<script type="text/javascript">
function foo()
{
alert("sss");
}
</script>
i am using vs2008

Categories