I have a website with many links on it at the moment.
So I wanted to ask if it is possible to add additionally to every "normal" link an extra link (for example www.google.com)?
I know this option:
<a href="http://virtual-doctor.net" onclick="window.open('http://runningrss.com');
return true;">multiopen</a>
But then I need to edit every single link on my website.
So is there a possibility to add a piece of code to the site and then every link on the website opens automatically (additionally to the normal linked page) an extra page?
I hope it's understandable what I mean :)
Greetings
Andrew
If I understand you well, you want to add a click event to all your links?
A bit weird request... but here is how you could do it:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
www.google.com
www.yahoo.com
www.gmail.com
<script>
function openWindow(e){
window.open('http://runningrss.com');
}
(function(){
var allAnchors = document.getElementsByTagName('a');
for (var i=0; i<allAnchors.length; i++)
{
var anchor = allAnchors[i]
//First remove existing in case already registered
anchor.removeEventListener('click',openWindow,false);
anchor.addEventListener('click',openWindow,false);
}
}())
</script>
</body>
</html>
Related
I wrote the following code in JavaScript to open a popup window when a button is pressed, and then use setInterval to move the window every 2 seconds.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset = "utf-8">
<title> Javascriptin' Some Codes </title>
<script>
function hi() {
var printOut = window.open("http://www.google.com","_blank", 'height=200, width=200');
setInterval(function() { printOut.moveBy(10,10);}, 2000)
window.alert("hi");}
</script>
</head>
<body>
<button onclick="hi()"> Try me </button>
</body>
</html>
The window opens, but the setInterval doesn't appear to work- the window does not move after being launched. I was wondering why my code doesn't work, and what I could do to make it function the way I'd like it to.
The opened url has to be on the same domain as stated in this answer (DEMO).
For example on jsfiddle, this works:
var printOut = window.open("http://fiddle.jshell.net","_blank", 'height=200, width=200');
And that one doesn't:
var printOut = window.open("http://www.google.com","_blank", 'height=200, width=200');
You should also remove the alert, although it works in chrome it seams to break it in for example opera.
*Update: Ultimately I've decided that accomplishing exactly what I want here isn't possible due to the issues it poses to security. Kalle's answer below gives a solution that is closest to what I want to accomplish.
In order to solve my problem I've created scripts on both pages and will use a sort of push notification that is routed through the server in order for them to communicate.
Thanks for the help!! *
I have two pages. Both windows already exist independently. Page two has a function declared in JS.
I would like to be able to call the function in window two by clicking a link in window one.
Page 1:
<html>
<head>
<title>This is a title!</title>
</head>
<body style="background: lightblue">
Click Me!
</body>
Page 2:
<html>
<head>
<META HTTP-EQUIV="Window-target" CONTENT="my_target" />
<title>This is a title!</title>
<script type=text/javascript>
function clicked() {
alert('test');
}
</script>
</head>
<body style="background: lightblue">
</body>
Since it is on the same domain you can get this to work but would have to change the way you were doing it a little.
First off you would have to open it in a popup using this syntax rather than a new tab:
newwindow=window.open(url,'name','height=200,width=150');
and then you could simply call newwindow.clicked() after the popup is called.
update
just did a quick test and this will open it in a new tab. (sorry its been a while since I used the open function.
newwindow=window.open(url,'name');
Just noticed also that you should wait for the popup to load. So in my Example it would look a little something like this (with jQuery):
var newwindow = window.open('http://www.tylerbiscoe.com/vb/new.html');
$(newwindow).load(function(){
newwindow.clicked();
});
Ok, brand new answer. I hope this is what you were thinking. This is however, when you open page 2 from page 1.. So basically, page 1 would know who page 2 is..
Online example: http://kopli.pri.ee/stackoverflow/6832271.php
Page 1
<html>
<head>
<title>Page 1</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<style>
.ajaxlink {color: blue; cursor: pointer; border-bottom: 1px dotted blue;}
</style>
</head>
<body>
<span id="open_page_2" class="ajaxlink">Open new window</span>
<br>
<br>
Click Me!
<script>
$('#open_page_2').click(function(){
child = window.open('test2.php','page_2','width=600,height=600');
});
$('a[target=my_target]').click(function () {
child.SecondPageFunction();
return false;
});
</script>
</body>
</html>
Page 2
<html>
<head>
<title>Page 2</title>
</head>
<body>
<h1>Your seeing page 2!</h1>
<script>
function SecondPageFunction () {
alert('Second page action got triggered!');
}
</script>
</body>
</html>
The script must be a part of the page you're opening in the new window. You're absolutely correct about it being a security flaw if it was elsewise allowed.
You could add some query string argument that could be picked up onload by javascript in the page you are opening and call your function if the query string arg is present.
Really getting in to javascript and looking around at some patterns. One I have come accross is the module pattern. Its seems like a nice way to think of chucks of functionality so I went ahead and tried to implement it with jQuery. I ran in to a snag though. Consider the following code
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>index</title>
<script type="text/javascript" charset="utf-8" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
var TestClass2 = (function(){
var someDiv;
return {
thisTest: function ()
{
someDiv = document.createElement("div");
$(someDiv).append("#index");
$(someDiv).html("hello");
$(someDiv).addClass("test_class");
}
}
})();
TestClass2.thisTest();
});
</script>
</head>
<body id="index" onload="">
<div id="name">
this is content
</div>
</body>
</html>
The above code alerts the html content of the div and then adds a class. These both use jQuery methods. The problem is that the .html() method works fine however i can not add the class. No errors result and the class does not get added. What is happening here? Why is the class not getting added to the div?
Ah, now that you've updated your question I can better answer your question. You should change the append to appendTo considering you're wanting to move the newly created element inside of the already present #index.
$(document).ready(function() {
var TestClass2 = (function() {
var someDiv = $("#name");
return {
thisTest: function() {
someDiv = document.createElement("div");
$(someDiv)
.html("hello")
.addClass("test_class")
.appendTo("#index");
}
}
})();
TestClass2.thisTest();
});
Hope this helps!
I copied and pasted your code and it works for me.
Make sure you're not simply viewing source to see if the class is applied because doing so simply shows you the HTML that was sent from the server - any DOM updates that occur through JavaScript will not be reflected.
To view the live DOM, use a tool like Firebug or WebKit's Inspector (comes built-in to Safari and Chrome).
Your code works great!
http://jsfiddle.net/lmcculley/p3fDX/
How can I do a script to catch strings as input and open them on a Firefox document? Each link would go to a different window or tab. Any ideas would be much appreciated.
I just want to be able to take some links and open them. For example I have 50 Links. And copying and parsing those 50 Links take a really long time and also a lot of work. If I can just write a script to read those links and let the computer do the work, it will be very helpful for me. I just don't know how to write that or where because it does not sound too hard (just gotta know how to). Thanks for any suggestions.
if i got you right, i guess you could do something like this. This will open the four urls listed but it will probably be blocked by the popup blocker.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Documento sin tÃtulo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script>
<!--
var dir = new Array();
dir[0] = "http://www.creativecorner.cl/";
dir[1] = "http://www.sourcing.cl/";
dir[2] = "http://www.feeds.cl/";
dir[3] = "http://www.neonomade.com/";
for(i = 0 ; i < dir.length ; i++){
window.open(dir[i],'autowindow' + i,'width=1024,height=768');
}
-->
</script>
</body>
</html>
Write this to a file names "links.html" on your hard disk:
<html>
<head><title>Your links</title></head>
<body>
Your links:<br />
XXX<br />
</body>
</html>
Replace the two "XXX" with one link and emit one "link" (a) line per link. You should be able to do that in most text editors with a little search'n'replace. After you're done, save the file and open it in your browser.
Another option is to look at the bookmark file of your browser and to duplicate the format. You can usually ignore things like "last visited", etc. Just add the links.
If you want to do this in JavaScript, you will need to use a form with a textarea. Create a small HTML document with a form, the JavaScript, the textarea and a div for the result.
Add a button which calls a JavaScript function which takes the text from the textarea, split it into lines and create the HTML above (only the link-lines) as a String. Now assign this string to the attribute innerHTML of the div to make the links clickable.
I would like to embed HTML in a SharePoint 2013 that creates two hyper-links using JavaScript.
As an example I'd like the follow page with two links:
Click Here for Pages. Click Here for Documents
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
Click Here for pages.Click Here for Documents.
</body>
</html>
The reason I want javascript is I will ultimately extend this to dynamically generate the URLs with different links.
I have zero experience in JavaScript but learn best from example. I have done a lot of investigation but cannot find a simple example like this.
Thanks in advance.
The easiest way to do it would be to assign each link an ID so that you can assign them to variables in the JavaScript, and then modify the href property of each link. From there you can either hard code them or load via AJAX. Here is how to hard code them.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
/*These variables allow you to reference and manipulate the specfic DOM elements
(ie. the a tags we gave ID's of link1 and link2)*/
var link1 = document.getElementById('link1'),
link2 = document.getElementById('link2');
link1.href = 'http://example.com/Pages';
link2.href = 'http://example.com/Documents';
</script>
</head>
<body>
<a id="link1" href="">Click Here</a> for pages.<a id="link2" href="">Click Here</a> for Documents.
</body>
EDIT: I threw the script into a script tag inside of the HTML. Not sure if that's what you were asking but this is how to integrate them together. Here is a live example:
https://jsfiddle.net/h1b4a3gb/
Just to complete the answer my original question to run this within a SharePoint Content Editor Web part I used the following code.
<a id="link1">Click Here</a> for pages.<a id="link2">Click Here</a> for Documents.
<script type="text/javascript">
var link1 = document.getElementById("link1"),
link2 = document.getElementById("link2");
link1.href = "http://example.com/Pages";
link2.href = "http://example.com/Documents";
</script>
The key difference here is the script must appear after the link definitions otherwise it will not work as the DOM object doesn't exist.