How to call javascript in href in sharepoint column - javascript

I do have a calculated column in sharepoint which creates an anchor tag in which href have a javascript function call to test method but it's not getting called what i am missing here.
Code :
<script type="text/javascript">
function test()
{
alert("hiii");
}
</script>
and calculated column :
<a href='javascript:test()'> Click </a>
Thanks in advance

Pure JS:
function test(){
alert("I am alerted");
}
<a onclick='test()' > Click </a>
jQuery
$(document).ready(function (){
$("#myID").on("click", function (){
alert("I am alerted !");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="myID" > Click </a>

If I understand, you need to change the display (html generated) of a field in SharePoint.
You are un 2013, you can use JSlink property of your field. With this, you can link a file JS to your field, and this file will be executed every time that sharepoint will generate the rendering of your field.
Check this link to understand how it work and see some exemples ;) : https://code.msdn.microsoft.com/office/Client-side-rendering-JS-2ed3538a
hope this will help you

The attribute in the anchor you are looking for is called onclick:
<a onclick="test()"> Click </a>
This should do the trick I guess.
Check the w3c-schools site for a more detailed description: W3C Schools on the onclick HTML attribute

Please try this way:
<a href='javascript:void(0)' onclick='test()'> Click </a>
<script type="text/javascript">
function test()
{
alert("hiii");
}
</script>

Related

a href=javascript:function() in firefox not working

I tried using a href=javascript:function() in a button, with a function to execute it. It works in Chrome but it doesn't work in Firefox.
Firefox doesn't alert and open blank tab.
Anyone can help me?
<script>
function verifyisbot() {
alert("test.");
var win = window.open("http://yahoo.com", '_blank');
win.focus();
}
</script>
Below is button code
<div class="frb_textcenter">
<a target="_blank" class="frb_button frb_round frb_center frb_fullwidth" href="Javascript:verifyisbot();">
click here
</a>
</div>
Update
I should have added that im using a live editor(profitbuilder) in wordpress to generate the page and button. There is no area for me to insert additional javascript onclick function to the button. So i figure out to use "ahref" blank field in the live editor to input javascript call function to fire up the function.
Is there any way i can make this work through the ahref without using onclick event? Or can i specify onclick function in the ahref field?
Sorry the test() is actually verifybot() function, typo mistake
You can achieve the goal using only the href attribute:
<a href="javascript:void(verifyisbot())" class="frb_button frb_round frb_center frb_fullwidth">
click here
</a>
It works, because when a browser follows a javascript: URI, it evaluates the code in the URI and then replaces the contents of the page with the returned value, unless the returned value is undefined. The void operator can be used to return undefined.
Use onclick event instead of a href=javascript.It works on firefox.See below:
<div class="frb_textcenter">
<a target="_blank" class="frb_button frb_round frb_center frb_fullwidth" onclick="test()">click here</a></div>
<script>
function test() {
alert("test.");
var win = window.open("http://yahoo.com", '_blank');
win.focus();
}
</script>
UPDATE 1: You can do it without use javascript.You just add the link in the href attribute.See below:
<a target="_blank" class="frb_button frb_round frb_center frb_fullwidth" href="http://yahoo.com">click here</a></div>
Give serious consideration to separating your JavaScript and your HTML such that the problem goes away. For instance, add an ID to your anchor and add an event handler through script:
<div class="frb_textcenter">
<a id="verify" target="_blank" class="frb_button frb_round frb_center frb_fullwidth" href="http://yahoo.com">
click here
</a>
</div>
Later...
<script>
function test() {
alert("test.");
var win = window.open("http://yahoo.com", '_blank');
win.focus();
}
window.onload = function () {
document.getElementById('verify').addEventListener('click', test);
};
</script>
Do note that with the example provided, you don't actually need JavaScript at all. The HTML itself will cause a new window/tab to open with Yahoo! loaded...

Stop some links (href containing certain string) from working

I'm attempting to stop some links on my site from working.
Here is my code:
Links:
<a href='http://mysite.home.com'>Home</a><br>
<a href='http://mysite.home.com/nextpage'>Next Page</a><br>
<a href='http://mysite.string.home.com'>Home 2</a><br>
<a href='http://mysite.string.home.com/otherpage'>Other Page</a>
Jquery:
<script>
$("a[href*='.string.']").click(function(e) {
e.preventDefault();
alert('You cannot use this link');
});
</script>
The goal is to stop the last two links (with href containing '.string.') from working and instead display the alert. However, this code isn't working and the links still work. What am I doing wrong?
The strange thing is the code works fine here: http://jsfiddle.net/TzUN3/364/
but not on my site. I have the script and links both in the <body> of my page, script after the links. Is this correct?
Thanks.
Forgot to add
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
to my page. Feel like an idiot.
Thank you all.
$(function() {
// Jquery is ready
// *= is contains
$("a[href*='.string.']").click(function(e) {
e.preventDefault();
alert('You cannot use this link');
});
});

Issue with on click in javascript?

How can we perform onclick event on href, I wrote the following code but ot does not perform any action. What's wrong with my code ?
Send Category Details
My Java script code is as follows,
function sendCategoryDetails()
{
alert("control is here");
}
html
Send Category Details
fiddle
use
Send Category Details
javascript:
has been disabled for security reasons in new versions of most browsers or its disabled by defualt
try this
Send Category Details
<script>
function sendCategoryDetails()
{
window.location.href("URL");
}
</script>
Please replace your anchor html to this
Send Category Details
OR using jQuery
Send Category Details
<script>
$(document).ready(function(){
$('#maillocation').click(function(){
sendCategoryDetails()
});
});
</script>
href attribute is used to add link.You cannot call the function on href
Send Category Details
href="#" will post the same page.It adds # in your url not good
href="javascript:void();" use this to avoid
Use javascript:document.location.href=function_name();
Check this Demo Fiddle
<script type="text/javascript">
function sendCategoryDetails(){
alert("control is here");
}
</script>
<a href="javascript:document.location.href=sendCategoryDetails();"
id="maillocation"
data-mini="true" data-role="button"
data-theme="b" data-inline="true">Send Category Details</a>
100% worked, dont know why minus vote!

javascript:void(0) in jquery

I have an jsp page with html and js like below. Now I want to keep the js function test() to another js file. I m trying to write that using jquery.
<div class="class1">
<a href="javascript:void(0)" onclick="test();" id="add_another" ><b>Add another user</b></a>
</div>
JS function
function test(){
alert("I am here");
//other code
}
I tried to write like this
$(function() {
$('#add_another').click(test);
});
But its not working. I am not sure if its because its having href="javascript:void(0)"
When the user clicks Add Another User link, it show display another div. That is the test() doing. But the alert itself is not coming.
Can somebody please help me in this?
To replicate javascript:void(0) you can use event.preventDefault() in js like this.
$(function () {
$('#add_another').click(function (e) {
e.preventDefault();
test();
});
});

Dynamically alter URL

Please how can I change the feed URL in this script with link(href)
<script type="text/javascript">
$(document).ready(function () {
$('#test').rssfeed('http://feeds.reuters.com/reuters/oddlyEnoughNews');
});
</script>
<div id="test"> </div>
See example here http://www.zazar.net/developers/jquery/zrssfeed/example.html
The test Div displays the content of the rss feed
I just want to be able to alter the rssfeed(url) by clicking links with diffrent rssfeed url
<a onclick ="rssfeed('http://feeds.cnn.com/News');"></a>
note sure how you'd want the link to load it, but I'm assuming on click so this is what I would do..
<script type="text/javascript">
$(document).ready(function () {
$('.rss').click(function(){
$('#test').rssfeed($(this).attr('href'));
})
});
</script>
<a class="rss" href="http://feeds.reuters.com/reuters/oddlyEnoughNews">load rss</a>
<div id="test"> </div>
As it is shown here: http://www.zazar.net/developers/jquery/zrssfeed/example_menu.html
One can simply change the feed by calling .rssfeed again.

Categories