Include URL with city parameter based on user's IP - javascript

The javascript code below works perfectly in the HTML of my site. It is a geo-target server that shows the city based on the user's IP.
<script src='http://promos.fling.com/geo/txt/location.php?testip='></script>
What I am doing is and I can not get it to work within a link I want to be generated according to the person's city. Example:
New York
I tried to work the way down but the response to the end of the URL after ?Q= is the code itself. I tried this:
New York
I have no experience with this, so any solution either in PHP or javascript would be welcome. I thank you!

In Jquery this is quite easy:
function cityUrl( ){
var url = "http://www.example.net/?q=";
var city;
$.get("http://promos.fling.com/geo/txt/location.php?testip=", function(data){
url += data;
city = data
}
var html = "<a href='" + url + "'>"+city+"</a>";
$( "div" ).append( html );
}
This generates a url based on the location, makes a link out of that url and sets the city as the clickable part of the link. Then this will be appended to the element specified (in this case div)
This might need some tweeking for your purposes but im sure you get the idea
I believe I was too much unclear about everything so I`ll try to clarify:
This is a function you need to call once for the page you want to show them on. Of course you cannot copy and paste this code and expect it to work.
Fiddle doesnt 100% work due to https and cross origin requests, but does reflect the idea

Related

How to pull token from wordpress url and give it a variable/value?

Let me explain exactly what i'm trying to do.
Someone lands on https://crypto.enzlo.com and submits their email. They get taken to the thank you page.
The thank you page url contains a bunch of tokens that looks like this:
https://crypto.enzlo.com/apply/?contactId=103&inf_contact_key=d5e172efa53b9940a898afcfa25596e21f32e5a885a1411d8c094e77aedca3ba&inf_field_BrowserLanguage=en-US%2Cen%3Bq%3D0.9&inf_field_FirstName=&inf_field_Email=heiko%40viceoffers.com&inf_4dAXudNU8407Jeuy=
I need to pull the 'inf_field_Email' token from the url and put it into a image tracking url that needs to fire so I can track the signup and the email. The image iframe pixel i need to fire on this thank you page looks like this:
you'll notice "track_id=inf_field_Email" in the iframe url. I need track_id to post the email back to me. So the variable/token inf_field_Email should be showing the email address from the thank you page url. So in this case the inf_field_Email would be heiko#viceoffers.com
I am thinking I will need to concatenate it into the url using JS, by giving it a variable/value. But not sure how to do that or how to then put that variable into the iframe image pixel that needs to fire on the same page...
Please let me know what you guys think is the best approach for this. I have been able to post the inf_field_Email as text on a page and have it display the email using a wordpress plugin but that plugin couldn't transfer that token into the iframe url.
We have to convert the URL into an object so we can call the properties. We can do accomplish this by using JSON.parse, and then concatenating the property to the iframe src, below is an example of what I would do.
$(document).ready(function() {
var search = location.search.substring(1);
search = JSON.parse('{"' + search.replace(/&/g, '","').replace(/=/g,'":"') + '"}', function(key, value) { return key===""?value:decodeURIComponent(value) });
console.log(search.inf_field_Email);
document.getElementById("govice").src = "https://govice.online/track/goal-iframe?goal_id=466&track_id=inf_field_Email=" + search.inf_field_Email;
});

Grab GET ajax calls from page just by providing URL

Here's an example of a page from the API I'm working with:
http://www.easports.com/fifa/football-club/apps/proclubs/PS4/CHIP%20IT%20BRO
On this page almost all of the data is being loaded in via GET calls.
Here are 3 of the main calls that I'm interested in:
http://www.easports.com/iframe/fifa14proclubs/api/platforms/PS4/clubs/66232/members
http://www.easports.com/iframe/fifa14proclubs/api/platforms/PS4/clubs/66232/info
http://www.easports.com/iframe/fifa14proclubs/api/platforms/PS4/clubs/66232/stats
You'll notice that there is one thing in common with these calls, and that is the number after the "clubs/" part of the URL. In this case, it's 66232. That is the ID of the club. Basically, if I have this ID, I can get all of the information I need from this API.
The problem:
The only way I can grab this ID is if I manually inspect the page myself via Firebug. On my website, users will need to be able to automatically register their clubs. I want them to be able to provide the URL to their club page, eg.:
http://www.easports.com/fifa/football-club/apps/proclubs/PS4/CHIP%20IT%20BRO
Is there any way I can grab the ID in these ajax calls just by having the URL of the page? I don't even need the info that is returned from these calls, I just need the club ID that is part of the URL of these calls, eg.:
www.easports.com/iframe/fifa14proclubs/api/platforms/PS4/clubs/66232/members
I can just use some string functions to grab the ID after the "clubs/" substring up until the next "/".
I've been looking all over for a solution but can't seem to find what I'm looking for.
Thanks in advance. :)
EDIT:
Why is my question getting downvoted like crazy? I think I explained it pretty well. :/
Try using PHP's DOMDocument:
// clubId.php
<?php
$dom = new DOMDocument; #$dom->loadHTMLFile('http://www.easports.com/fifa/football-club/apps/proclubs/PS4/CHIP IT BRO');
$bod = $dom->getElementsByTagName('body');
if($bod = $bod->item(0)){
$cid = $bod->getAttribute('club-id');
$ea = 'http://www.easports.com/iframe/fifa14proclubs/api/platforms/PS4/clubs/'.$cid;
$members = #file_get_contents("$ea/memebers");
$info = #file_get_contents("$ea/info");
$stats = #file_get_contents("$ea/stats");
}
else{
echo 'Sorry, the Page is Probably Blocked!';
}
?>
Now you can echo $members, and the like, into HTML that creates JavaScript. Of course, you should have a firm grasp on JavaScript Objects and Arrays.
IT DOES APPEAR BLOCKED THOUGH!!!

How Can I Insert A Value With Javascript (or PHP) Into a Form Depending On Visitor Referral

I'm not sure if the way to do this is check Google Analytics cookies or otherwise track where a user came to my site from. Basically I have a form with a hidden field code="XY1" Now I need to be able to insert a different preset code for say people who came from Facebook, so the script would have to check where the visitor came from and then assign a code XF1 to any from FB, and a code XT1 to any from Twitter, etc.
Would something like this PHP work for the capture?:
$referringPage = parse_url( $_SERVER['HTTP_REFERER'] );
if ( stristr( $referringPage['host'], 'facebook.com' ) )
Or this JS
var ref = document.referrer;
if (!ref.indexOf("facebook.com") != -1) {
document.write(...)
}
I'm not sure what is the best way to do it and what kind of methods can reliably check the source of a visitor, so any help would be greatly appreciated.
You can use $_SERVER['HTTP_REFERER'], but it's not guaranteed to be accurate, or even present. Not all browsers will necessarily set it, and some allow you to set it yourself. Google cookies won't contain any site history, and you can't examine the browser history, so there's no guaranteed way to do what you're asking.
You can try this option using jquery $.test() method.
$(function(){
var referer=document.referrer, //option 1
//referer="<?php echo $_SERVER['HTTP_REFERER'];?>",//optional 2
XFB=/facebook.com/g,
XFT=/twitter.com/g,
checkF1=XFB.test(referer),
checkF2=XFT.test(referer);
if(checkF1){
var code= "XF1";
$('#hiddenInput').attr('value','ref: '+referer)
}
else if(checkF2){
var code= "XT1";
$('#hiddenInput').attr('value','ref: '+referer)
}
});

Displaying a localstorage variable

This question pertian to a Chrome Browser Extension (js Injection and localstorage)
Okay, I swear I have searched for days for the answer to this question (or one similar) and either there is not one or I am a complete moron about this topic.
Here is what I'm trying to do and it should be something very simple. I am not new to chrome extensions but VERY new to creating options for the addon....
I simply want to display a saved piece of data visually. For example...
Let's say the localstorage has stored an input text field (username) as "MyUsername".
I want to recall and display that visually in the page that i am injecting code to. Here is an example...
The Code (inject.js):
var el = document.createElement('div'),
b = document.getElementsByTagName('body')[0];
otherlib = false,
el.setAttribute("id", "vBar");
el.style.position = 'fixed';
msg = 'just testing';
return showMsg();
Where you see "msg = 'just testing';"... I just want it to display the username (from local storage) instead of 'just testing'.
The Output should just look like...
MyUsername
Please tell me I'm not an idiot. I can't figure out why it's not displaying the result. It simply shows...
Undefined
When a content script accesses localStorage, it will access the storage belonging to the domain of the page where the script was injected. That's probably not what you want in this case.
You can use the chrome.storage api instead, that will allow you to share information across all your extension scripts instances and your background page.

Using jQuery on a string containing HTML

I'm trying to make a field similar to the facebook share box where you can enter a url and it gives you data about the page, title, pictures, etc. I have set up a server side service to get the html from the page as a string and am trying to just get the page title. I tried this:
function getLinkData(link) {
link = '/Home/GetStringFromURL?url=' + link;
$.ajax({
url: link,
success: function (data) {
$('#result').html($(data).find('title').html());
$('#result').fadeIn('slow');
}
});
}
which doesn't work, however the following does:
$(data).appendTo('#result')
var title = $('#result').find('title').html();
$('#result').html(title);
$('#result').fadeIn('slow');
but I don't want to write all the HTML to the page as in some case it redirects and does all sorts of nasty things. Any ideas?
Thanks
Ben
Try using filter rather than find:
$('#result').html($(data).filter('title').html());
To do this with jQuery, .filter is what you need (as lonesomeday pointed out):
$("#result").text($(data).filter("title").text());
However do not insert the HTML of the foreign document into your page. This will leave your site open to XSS attacks.
As has been pointed out, this depends on the browser's innerHTML implementation, so it does not work consistently.
Even better is to do all the relevant HTML processing on the server. Sending only the relevant information to your JS will make the client code vastly simpler and faster. You can whitelist safe/desired tags/attributes without ever worrying about dangerous ish getting sent to your users. Processing the HTML on the server will not slow down your site. Your language already has excellent HTML parsers, why not use them?.
When you place an entire HTML document into a jQuery object, all but the content of the <body> gets stripped away.
If all you need is the content of the <title>, you could try a simple regex:
var title = /<title>([^<]+)<\/title>/.exec(dat)[ 1 ];
alert(title);
Or using .split():
var title = dat.split( '<title>' )[1].split( '</title>' )[0];
alert(title);
The alternative is to look for the title yourself. Fortunately, unlike most parse your own html questions, finding the title is very easy because it doesn;t allow any nested elements. Look in the string for something like <title>(.*)</title> and you should be set.
(yes yes yes I know never use regex on html, but this is an exceptionally simple case)

Categories