Link randomizer? - javascript

Well I thought of this one and I have no ideas how it can be made... I thought to search up for help over here. I am just html/css newbie programmer and I dont know to make javascripts or scripts with jquery help ...
Anyway... This is my explanation.
Lets say I have an download button.
<a onclick="run"><div id="download-button"></div></a>
There will be rest in css for normal and active state... Lets skip it here.
Now what I need is javascript I guess?
I want that onclick "run" to trigger that script where I can copy/paste a couple of links for download...
Lets say I have 5 links that redirects to eg.(rapidshare)...
I wanna paste this five links in that javascript, so download button randomly pick one link. Example... Downloading link no 4.
So if I keep clicking on it it will randomly redirect to one link... 1,2,3,4,5 doesnt matter :) (if possible, it wont repeat same link again). If not possible, repeating same link is fine.
Is this even possible? I guess some math count things in javascript or something :D I dont know how javascript works so I cannot make, really.
I also know that this site is not "Come, ask question and get everything done"... But please, if you just can help me with this javascript trouble? I dont even know to write anything except <script type="text/javascript"></script>
Thanks in advance!

Put all your links inside an Array (assume myArray), then you can call:
var randomItem = myArray[Math.floor(Math.random()*myArray.length)];
To select a random item from that array.
Reference + details

Here you have a jquery solution:
HTML:
<div id="button">
link
<a class="button-link" href="http://www.google.com">a</a>
<a class="button-link" href="http://www.yahoo.com">b</a>
<a class="button-link" href="http://www.bing.com">c</a>
</div>
CSS:
#button {
background: red;
}
#button a.button-link {
display: none;
}​
Javascript:
$(function() {
$("#button").click(function () {
var $anchors = $(this).find("a.button-link");
var $anchor = $($anchors[Math.floor(Math.random()*$anchors.length)]);
window.open($anchor.attr("href"),'_blank');
});​
});
And last, but not least, working demo.

Related

How to use Javascript to change href and html text?

So I've got this little piece of HTML that I have zero access to, and I need to change the URL of where it's linking, to somewhere else.
Now I've looked around, and I've tried different approaches and non seem to work so I must be doing something wrong.
the Html code:
<div class="manageable-content" data-container="edit_register_ind_container">
<a class="entry-text-link secondary-step step-button" id="register_ind_container" href="oldurl">Register</a>
</div>
First I wanted to try something that seemed easier, which was to change the displayed text "Register" to "Start a Fundraiser"
This is what I have got for that part:
// url manipulation
$(document).ready(function(){
$("#register_ind_container").click(function(){
$("#manageable-content a").text('Start a Fundraiser');
});
$("#register_ind_container").attr("href", "http://google.ca");
});
No luck so far for any of it.
a little background information:
I am using a platform called Luminate/Blackbaud, its a CMS with a weird set up. header tags and stuff like that go in a different place than the html body and the css is somewhere else as well (but I'm just using ftp to reference it in the header).
How I'm referencing the javascript code.
<script type="text/javascript" src="../mResonsive/js/urlmanipulation.js"></script>
My css works so I'm certain this should to, but I just don't know why it isn't.
All suggestions welcome (except for asking for the html access because I have, 3 weeks ago lol)
Thank you for your time!
I saw your both code :
$("#register_ind_container").attr("href", "http://google.ca");
This line will execute on page load so href should be changed on load
$("#register_ind_container").click(function(){
But when you performing this click on Id
it wont work because at that instance this id associated with an hyperlink
so hyperlink having the default subset rules
for Overriding this you can try
$("#register_ind_container").click(function(e){
// custom handling here
e.preventDefault();
$(this).text('Start a Fundraiser');
});
But this is also not a Good Practice. Hope this helps !
$(document).ready(function(){
$("#register_ind_container").click(function(){
$(this).text('Start a Fundraiser');
$(this).attr("href", "http://google.ca");
});
});
You are changing the URL outside the click event.. Wrap it inside the click event.. Also make use of $(this)
// url manipulation
$(document).ready(function(){
$("#register_ind_container").click(function(){
$(this).text('Start a Fundraiser').attr("href", "http://google.ca");
});
});

JQuery Toggle Divs Based On Index not retoggle when clicked twice

and thank you for reading this question. Let me preface this by saying that I am not a programmer and only have tried to learn javascript to make my own websites look and function the way I want.
I have a page with several hidden divs. I'm using elements with the same class and different targets to trigger this Jquery
jQuery(function () {
jQuery('.nav').click(function () {
var index = $(this).index(),
newTarget = jQuery('.targetDiv').eq(index);
jQuery('.targetDiv').not(newTarget).slideUp('fast')
newTarget.delay('fast').slideToggle('fast')
return false;
})
});
So my ".targetDiv"s look like this:
<div class=".targetDiv" style="display:none">div1</div>
<div class=".targetDiv" style="display:none">div2</div>
<div class=".targetDiv" style="display:none">div3</div>
And the "navigation" would look something like this
link1
link2
link3
This is not my code, and I got it from here: http://forum.jquery.com/topic/slidetoggle-multiple-divs-31-5-2013
It works exactly as it is supposed to and I have no complaints about that. When you click on a link, the corresponding div toggles, but when you click the same div again right afterwards, it toggles again and slides up (which is how the code is written). I want to stop that from happening, and since I am new to Javascript and Jquery I can't figure out how to do it. My non programmer mind assumes that there should be some kind of if else clause, where you would say:
if .targetDiv is :visible, then do not toggle newTarget. However when I tried to do that, it did not work.
if($(".targetDiv").is(":hidden")) { jQuery(function () {
jQuery('.nav').click(function () {
var index = $(this).index(),
newTarget = jQuery('.targetDiv').eq(index);
jQuery('.targetDiv').not(newTarget).slideUp('fast')
newTarget.delay('fast').slideToggle('fast')
return false;
})
});}
else {alert("already open")}
I don't know how else I should handle this, but it must be possible and I am probably just thinking of how to achieve what I want in entirely the wrong way. I understand very little about javascript, but I am not asking for someone to write this for me, I'd rather have someone tell me what it is that I am doing that is incorrect, then explain what it is I should be trying to do. Then I can use google to search for the way to achieve that.
Again, thank you for taking the time to read this and hopefully I've been detailed enough for some answers.
You just need to wrap the two 'slide' lines in the if statement, like so:
if (!newTarget.is(':visible'))
{
jQuery('.targetDiv').not(newTarget).slideUp('fast');
newTarget.delay('fast').slideToggle('fast');
}
You may also want to fix a few issues with the html, e.g. take the periods out of your class names. When querying for DOM elements, the "." means, "Find the things that have a class called _[whatever follows the dot]". Don't put dots in the classes themselves.
You may also want to take out the href attributes of the <a> tags. They aren't necessary.
Here's a working JSFiddle. Cheers!

Javascript,Jquery does not work when i change the tabs

I apply the below script to add color to the comments I add in my application (JIRA).
<script type="text/javascript">
$(document).ready(function() {
$(".activity-comment:even").css("background-color","#6699FF");
$(".activity-comment:odd").css("background-color","#B2CCFF");
});
comments http://i.minus.com/jFoE7kcaTqdrp.JPG
After applying the script the comments appear as above , however the script does not work when i move to other tabs (i.e. Worklog , History) or i add a new comment. I need to refresh the page for the script to execute .
Can someone please help me how i can permanently set the colors ?
Note : I am applying the javascript on the application announcement banner this makes the script run on every page of the application , I can make changes to the source files.
Thanks in Advance :)
I think you can achieve this thing in 2 ways:
1- By using CSS: In your css file just add following lines of code:
.activity-comment:even{
background-color: #6699FF;
}
.activity-comment:odd{
background-color: #B2CCFF;
}
2- By Jquery: In this case use .live() function of jquery to achieve style on dynamic added comments. Like get the id of comment submit button (suppose: comment_add) , and then:
$("#comment_add").live('click',function(){
$(".activity-comment:even").css("background-color","#6699FF");
$(".activity-comment:odd").css("background-color","#B2CCFF");
});
I think it will help you.

How to show loading image(GIF) until Script function is Finished

I have a html button. Code is given bellow,
<div id="apDiv1">
<input name="wpost" type="button" value="Publish" onClick="wall_publish()"/>
</div>
When a user click 'Publish' button it calls 'wall_publish()' script function which is in the same page.Code of the 'wall_publish()' function is given bellow,
<script>
function wall_publish(){
//some codes...
}
</script>
I need to display a loading gif image near my 'Publish' button until Script function is finished.
Can anyone please tell me how to do this?
you should show the picture do the job then remooove it again just that simple
function show pic();
function do job();
function remove pic();
If you have no idea about how to show a picture i think you should go check java-script doc's
if you try to make the pic invisible i can suggest you use the jquery library and just addthe line
$('#img').hide(); to hide it and $('#img').show(); to show it ;
this is not very specific but you sshould go throu that
i think you HTML code but not sure
<img src="the adress of the image" id="gif" />
The best way to show/remove any html is by making use of the display property.
Create a hidden image.
function wall_publish(){
document.getElementById('loading').style.display='inline'; /* display it */
// do what you need here
document.getElementById('loading').style.display='hidden'; /* put it away again */
}
There are, of course, a dozen way to do this. I picked the most straight-forward easy approach that I used to use when I first started to learn javascript.

Javascript: Page reloads with eventListener click

In trying to understand javascript best practices, I'm attempting to recreate a piece of inline javascript by adding an event listener from an external javascript file.
The inline code works fine and looks like this:
<p id="inline" align="left">
This is a body paragraph which changes alignment
when a user clicks on a link below
</p>
<p>
Align Right
</p>
Concerning my problem, the important thing to note here is that return false; prevents the page from reloading (I'm not actually sure why, and wouldn't mind finding out, especially if it relates to the solution to my problem...). This is what I want. I don't need the page to reload to move the text to the right.
However, I have no idea what the best way to keep the page from reloading is when my javascript is in an external file. Here's what my first attempt looks like. I started with html that looks like this:
<p id="external" align="left">
This is a body paragraph which changes alignment
when a user clicks on a link below. It uses an
external .js file.
</p>
<p>
Align Right
</p>
And javascript that looks like this:
function alignListener () {
document.getElementById('external').setAttribute('align', 'right');
}
function installListeners () {
var aRight = document.getElementById('aRight');
aright.addEventListener('click', alignListener, false);
}
This almost works, but not at all how I would expect. When I click on the 'Align Right' link, the text briefly aligns right, but then, I follow the link to the current page, which resets the alignment back to the left.
I found a way to sort of fix that problem, by using <a href="#" ... instead of <a href="" .... While this doesn't reload the page (so the text stays aligned), it does take me to the top, which isn't really what I want. I'd like a solution similar to the return false; that works with the inline javascript. Is there a simple way to do this? Or am I doing it wrong completely?
I highly recommend the mozilla developer network for most of these types of answers. It's easy to read and will help you understand JavaScript and the DOM. (JavaScript is good!, DOM is awkward...)
Specifically, for events: https://developer.mozilla.org/en/DOM/event
In general, https://developer.mozilla.org/
There are a few ways to stop events, preventDefault(), stopPropagation(), return false;, or use a JavaScript framework as suggested above. jQuery is good, there are many many others out there (YUI, Dojo, MooTools, etc.), and they all endeavor to make your JavaScript more compatible with different browsers.
Use <button> instead of <a> tag.
That helped me.
You can use:
function event(e){
var e=window.event||e;
//do stuff
if(e.preventDefault){e.preventDefault()}else{e.returnValue=false}
}
Note that this doesn't need to go on a new function: .addEventListener("click",function(e){/*the above function*/})
Cross-compatible event listener:
if(element.addEventListener){
element.addEventListener(eventName, function, false);
}else{
element.attachEvent("on"+eventName, function);
}

Categories