Retrieve link address from Span Class in table - javascript

I'm currently learning to code in javascript and jquery so my knowledge is exceptionally poor. Im trying to edit a script(with lots of googling) that was written in tampermonkey by a friend and I assume I havent been able to find the answer through search as I have been using the incorrect terms - but dont know what they are.
this is the HTML
<span class="village_anchor contexted" data-player="9281645" data-id="804">
Black Reaper (492|489) K44
<a class="ctx" href="#"></a></span>
All I want to do is get the address that links to and then use location. to go there.
Once again, apologies for my lack of prowess and thankyou for any help.

All you have to do, is to select the spanby his class and then finds the a tag you want.
So you can write this:
var linkhref = $(".village_anchor").find("a").first().attr("href");
Then, you can do the "redirection":
document.location = linkhref;

Use this code.
var address = $('span.village_anchor.contexted a:eq(0)').attr('href');
window.location.href = address; //to open the link in same page.

Related

Change all external links using jQuery

I want to change all external links on my blog (blogspot here, that's why I'm looking for jQuery code) without changing the posting of my blog because I need a lot of work if I do that.
For example, my website is example.com.
I want to change all external links to
http://example.com/p/go.html?url=http://externallink.com
without need for any changes on my blog post. I don't have any idea to start with.
SOLVED: https://gist.github.com/2342509 Thanks everyone :D I just need to change it a bit.
In jQuery you can try:
// DOM ready
$(function(){
$('a[target="_blank"]').prop('href', 'http://example.com/p/go.html?url=http://externallink.com');
});
Ofcourse this will only work if you have set the target="_blank" property/attribute in HTML and if you want all links to open the same url. This idea derives from the fact you want to have external links open automatically in a different tab/window.
If this is not the required functionality, you can use a custom data- attribute in a similar way. Only difference is you will need to loop each link, and get the data from it.
// DOM ready
$(function(){
$('a[data-href]').each(function(){
var anc = $(this),
href = anc.prop('href'),
dataHref = anc.data('href');
anc.prop('href', href + '?url=' + dataHref);
});
});
HTML example:
external link
And now you will probably need to add more information if that is still not what you want.
Going off of #Tim Vermaelan's answer, you could try this, which will check for every link that doesn't start with your website's URL without relying on it being target="_blank":
$('a:not([href^="http://yoursite.com"])').prop('href', 'http://example.com/p/go.html?url=http://externallink.com');

Load a specifc source in iframe based on URL

I am a very, very beginner at coding. And I think I am close to achieving something, but just can't get it to work just right.
Our company loads our product database into a specific program that is searchable by customers in our industry. Until I figure out how to tie into the API they provide, our website lists all of our products through iframes.
Quite often I have a client asking me to provide them a direct link to a specific product number. I'd rather not try to create a separate page with an iframe specifically linked to that one product, so I've been searching online for how to accomplish all of this.
My site is promo-central.com
I built a special search page at promo-central.com/searchby.html
I am hoping for some way to be able to give my client a link to a product (SBP26 for example) in the form of something like www.promo-central.com/searchby.html?SBP26
That will then tell the iframe on that page to load the src based on that extra info in the URL.
Here's what I currently have:
Link that i'd like to work: http://promo-central.com/searchby.html?itemnum=sbp26
The URL that should be called in this situation to correctly load the iframe would be http://www.promoplace.com/ws/ws.dll/StartSrch?DistID=36182&itemnum=sbp26
Here's the code on the page:
<iframe height="100%" name="WE_Frame" id="WE_Frame" src="http://www.promoplace.com/ws/ws.dll?DistID=36182"></iframe>
<script>
(function() {
var frameBaseSRC = document.getElementById("WE_Frame").src;
var frameQueryString = document.location.href.split("itemnum=")[1];
if (frameQueryString != undefined) {
document.getElementById("WE_Frame").src = frameBaseSRC + "&itemnum=" + frameQueryString;
}
})();
</script>
To me this looks like it should work, but it doesn't work for me.
Am I way off? Or do I just have some kind of small issue?
I REALLY appreciate any help, or alternate ideas you may have.
Thanks in advance!
You are missing "StartSrch" from the base iframe src that you are trying to append the query string to. Your iframe just needs to be updated like this:
<iframe height="100%" name="WE_Frame" id="WE_Frame" src="http://www.promoplace.com/ws/ws.dll/StartSrch?DistID=36182">

How to send a value from a search box to a URL?

I have a search box and I have no clue how to append the string that the user searches to url after a user clicks search.
How would this work?
I have a reference code but I want to know how to append the value that a user searches:
onclick="location.href = $(this).attr('href')+'?q1=Asustablet&x=70&y=14=siys';return false"
Here is my jsFiddle
yu can't use jquery inline. you can use its events like this:
$('#search-button-4').click(function(){
window.location.href = $(this).attr('href')+'?q1=Asustablet&x=70&y=14=siys';
});
DEMO
May I suggest using PHP as it will be much easier,
Add this inside PHP tags and set the variable for $user_search_input
header("Location: ".$_SERVER['REQUEST_URI']."?search=".$user_search_input);
I'm sorry if you needed a Javascript version but if not I highly suggest PHP for it has a lot of functionality being very powerful and has a lot of potential.

how to get variable's value from URL and pass to all the links on the whole site?

I am trying to get a variable from a URL,
for example if the users can link to this page: www.example.com?var1=blabla&var2=blabla
and there are many links on this site www.example.com.
What I want is no matter where my users click, they will always see the link appended with 2 variables they got from this link www.example.com?var1=blabla&var2=blabla
for example: if there is another link
<a href="www.example.com/page1.php"/>Go to page1</a>
on www.example.com?var1=blabla&var2=blabla, they will go to page www.example.com/page1.php?var1=blabla&var2=blabla.
This is my code:
<script type="text/javascript">
$(document).ready(function() {
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1);
var links = $('a');
links.each(function(){
var curLink=$(this);
var href = curLink.attr('href');
var newhref = href +'?'+ hashes;
curLink.attr('href',newhref);
});
});
</script>
My question is: I can only make all the links on the same page append those 2 variable. If the users go to another page, it won't do the same thing, because there is no script on it... but I can't add same script on every pages...
And another question, I want to clear the things after question mark and append the new ones.. is that possible to do that?
Thanks!
Your code looks good to me. And from your question it appears that your problems lie outside this.
My question is: I can only make all the links on the same page append
those 2 variable. If the users go to another page, it won't do the
same thing, because there is no script on it... but I can't add same
script on every pages...
If you can't run your script on every page then you can't modify the url's. You will need to do it server side. If you can't do it server side then you are out of luck.
And another question, I want to clear the things after question mark
and append the new ones.. is that possible to do that?
Yes, that is certainly possible. To strip the querystring use the following code.
var strippedHREF = window.location.href.substring(0, window.location.href.indexOf('?'));

Javascript if url equals function for adding text over only one page

What I want to do is pretty simple. Above my blog I want to have a text paragraph with an explanation of the site. However I do not want it to be at the top of every page on the site ONLY the index page. My host has a very restrictive format. I've already asked them for help and it isn't possible with their editor. I cannot edit all of the html but I can change some. Javascript seems to be allowed so I have been trying things like:
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--
if (url==http://mysiteaddress/index.html)
{document.write('Welcome to my site')}
//-->
</SCRIPT>
Taking out the if statement, it successfully writes 'Welcome to My site'. So I'm wondering what is wrong with the if statement. I've also tried adding an 'else' in order to see output on any page but have not had any luck.
I obviously don't know much yet so any help would be appreciated. Thanks!
The url is available via location.href - not sure why you think url exists.
Besides that, mime types are lowercase and the language attribute is obsolete. So use <script type="text/javascript">.
Additionally it'd be better if you didn't use the infamous document.write but document.getElementById('someDiv').innerHTML = 'your html here';
Or get jQuery and simply write $('#someDiv').html('your html here');
Just to add, you can prepend an element to the body element like this:
var p = document.createElement("p");
p.innerHTML = "I am a paragraph";
document.body.insertBefore(p, document.body.firstChild);
You can try it here.
Reference: https://developer.mozilla.org/En/DOM/Node.insertBefore

Categories

Resources