how check if link go for certain site using javascript - javascript

how check if link go for certain site using javascript
function checkLinks() {
var anchors = document.getElementsByTagName('a');
for (var i=0; i<anchors.length; i++){
var anchor = anchors[i];
if (anchor.href == "http://google.com"){/*compare not working*/
alert("exist");
}
}
}

Try using anchor.getAttribute('href').

You could use regexp to try and match the links. With this method, it checks if it is directing to that domain (what exact url doesn't matter, as long as it has "google.com" somewhere in the URL):
function checkLinks() {
var anchors = document.links;
for (var i=0; i<anchors.length; i++){
var anchor = anchors[i].href;
var re = new RegExp("google\.com","ig");
if (re.test(anchor)){
alert("exist");
}
}
}
example: http://jsfiddle.net/niklasvh/ELg6d/

Your compare isn't working because that's not what .href is returning. I'm guessing if you looked at the actual value of href, it would have another '/' on the end.

Related

sending stored javascript variables using href to a new url

Im trying to get a stored variable that came in a url to be used as a variable with href.
The code I have so far is:
function GetUrlValue(VarSearch){
var SearchString = window.location.search.substring(1);
var VariableArray = SearchString.split('&');
for(var i = 0; i < VariableArray.length; i++){
var KeyValuePair = VariableArray[i].split('=');
if(KeyValuePair[0] == VarSearch){
return KeyValuePair[1];
}
}
}
And this works
http://www.somesite.com/?title=1254&keyphrase=phase1
arrives to the page with the script and it does store the values for title and keyphrase.
What Im trying to do is to build a href that in the end has a link like this:
http://www.anewsite.com/1254/
The domain will never change - only the variable at the end.
Is this possible ?
Any help will be most appreciated.

Trying to remove links from variable

$.get("http://en.wikipedia.org/wiki/Afghanistan", function(response) {
var elements = $.parseHTML(response);
var wiki = $(elements).find('#mw-content-text').find("p");
var ps = [];
var arrayLength = wiki.length;
for (var i = 0; i < arrayLength; i++) {
if (wiki[i].innerHTML === "") {
break;
}
var item = wiki[i]
ps.push(item);
$("#fakediv").append(ps);
}
I am trying to remove the links from the variable item, but I can't seem to find any examples of how to do this from a variable - everything assumes that I'll be using a selector.
I've tried .removeAttr("href"), but that doesn't seem to work and I'm not quite sure how to remove links and leave the text.
You say you want to unlink the links, but you are looping over paragraph elements and trying to remove its attribute. I doubt the paragraph has an href attribute.
So you need to find the anchors inside the paragraph tags
var item = wiki.eq(i);
item.find("a").removeAttr("href")
ps.push(item);
or
var item = wiki.eq(i);
item.find("a").contents().unwrap();
ps.push(item);

Apply javascript just in a div?

I have coded a few lines which extract the URL of all the img tags in the page.
Ok everything works well but I wonder if there is a way to apply my code in a special div in the page!!!?
I mean I want to get URL of img tags from a div not from the whole page body.
I hope that I explained clearly what I meant :)
Any solution would be appreciated
function getURL() {
var url = [];
var a = document.getElementsByTagName('img');
for (var i=0, j = a.length; i<j; i++)
{
if (/\.(jpg|jpeg|gif|png)$/im.test(a[i].getAttribute('src')))
{
url.push(a[i].getAttribute('src'));
}
}
document.write(url);
}
Replace document.getElementsByTagName('img') with yourElement.getElementsByTagName('img');
Change your function to accept a startElement parameter, then call the getElementsByTagName on the passed element
function getURL(startElement) {
var url = [];
var a = startElement.getElementsByTagName('img');
for (var i=0, i < a.length; i++)
{
if (/\.(jpg|jpeg|gif|png)$/im.test(a[i].getAttribute('src')))
{
url.push(a[i].getAttribute('src'));
}
}
return url; // return the result instead of writing it
}
Say you have this markup
<div id='myDiv'><img src='test.jpg'/></div>
You could then call
var urls = getUrl(document.getElementById('myDiv'));
Also I suggest not using document.write, open dev tools (usually F12) and use console.log instead,
console.log(urls);

How to get hash tag in javascript SRC

Is there easy way to get the hash tag in script source?
Example:
<script src='myscript.js#result=5235'></script>
I want to get the variable result=5235 using myscript.js
http://jsfiddle.net/cjc343/h6Zzw/
You'd probably want to use a more specific selector than that, but it gets the point across.
Edit: Why did you tag jQuery if you don't want jQuery?
Pure: http://jsfiddle.net/cjc343/h6Zzw/1/
var hash = $('script').attr('src').split('#')[1];
or
var hash = document.getElementsByTagName('script')[0].src;
hash = hash.split('#')[1];
assuming it's the first script, otherwise change [0] to whatever index the script has.
var srcVariable = $(script).attr("src");
var hashText = srcVariable.substring(srcVariable.indexOf('#'));
For this example suppose you want to do your operation of getting that result=whatever to all of your script tags on your page. Do something like below:
var whatever = [];
$('script').each(function(){
var src = $(this).attr('src');
var splitonhash-arrReturned = src.split('#');
var yourAnswer = splitonhash-arrReturned[1];
whatever.push(yourAnswer);
});
PURE JS example:
var whatever = [];
var items = document.getElementsByTagName('script');
for (i=0; i< items.length; i++)
{
whatever.push(items[i].attributes.getNamedItem("src").split('#')[1]);
}

find specific links that contain a word and add to array

I am trying to search a page for links that contain the word playgame. If they found then I add them to an array. After that Select a random value from the array and use window.location. My problem is that it says my indexof is undefined. I not sure exactly that what means as I am still learning to use this feature of javascript.
example of link
<img src="http://games.mochiads.com/c/g/running-lion-2/_thumb_100x100.jpg">
javascript
var gameLinks = document.getElementsByTagName("a");
if (gameLinks.href.indexOf("playgame") != -1) {
var links = [];
links.push(gameLinks.href);
var randomHref = links[Math.floor(Math.random() * links.length)];
window.location = randomHref;
}
My problem is that it says my indexof is undefined
Not indexOf, the thing you're calling it on. gameLinks is a NodeList, it doesn't have an href property. You need to loop through the contents of the list to look at the individual element's href property. E.g.:
var index, href, links, randomHref, gameLinks;
gameLinks = document.getElementsByTagName("a");
// Loop through the links
links = [];
for (index = 0; index < gameLinks.length; ++index) {
// Get this specific link's href
href = gameLinks[index].href;
if (href.indexOf("playgame") != -1) {
links.push(href);
}
}
randomHref = links[Math.floor(Math.random() * links.length)];
window.location = randomHref;
More to explore:
DOM2 Core
DOM2 HTML
DOM3 Core
HTML5 Web Application APIs

Categories