Can anyone give me a solution on How can I load the Google plus share button (not the +1 button) in an iframe asynchronously. I have managed to do that for the +1 button.
Try this html snippet:
<iframe src="https://plusone.google.com/_/+1/fastbutton?bsv&size=medium&hl=en-US&url=http://test.com/&parent=http://test.com/" allowtransparency="true" frameborder="0" scrolling="no" title="+1"></iframe>
If what you're trying to do is explicitly render the button when a user hovers over something, what you need to do is use the explicit render flow as described here:
https://developers.google.com/+/plugins/+1button/
However, there's a twist, because you're using a share button, you can't use the explicit render code to render directly to the div. Instead, you will create your share link, set the render to explicit, and then can use JavaScript to trigger the rendering of the share button.
The relevant code is:
<html>
<head>
<title>+Share: Explicit render</title>
<link rel="canonical" href="http://www.example.com" />
<script type="text/javascript">
window.___gcfg = {
lang: 'en-US',
parsetags: 'explicit'
};
function renderShare() {
gapi.plus.go(); // Render all the buttons
}
</script>
</head>
<body>
Render the share control
<!-- a share button that will get rendered when gapi.plus.go is called -->
<div class="g-plus" data-action="share" id="share-div"></div>
</body>
<script type="text/javascript">
// Asynchronously load the plusone script, for performance
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(po, s);
})();
</script>
</html>
For you, I am guessing that you're trying to use a different trigger for rendering your +1 button than a button the user explicitly has to click, you could just use the appropriate event for what you want to trigger the render.
Related
We have a Line it! share button on our page, it is blocking loading of our own resources with is causing a big performance issue.
With the implementation of the Line Share button proposed by line.me their line-button.js script needs to be included in the middle of the page, where ever the button should be shown. The button replaces the <script> element from where it's initiated:
<script type="text/javascript">
new media_line_me.LineButton({"pc":false,"lang":"en","type":"b"});
</script>
I can add the line-button.js script to the page after our own have completed:
function includeLine(callback) {
var script = document.createElement('script');
script.async = true;
script.onload = callback;
script.src = "//media.line.me/js/line-button.js?v=20140411";
document.body.appendChild(script);
}
Then I can inject the script element like this:
function initLine() {
var
script = document.createElement('script'),
line = ['new media_line_me.LineButton({"pc":false,"lang":"'];
line.push(util.getLang());
line.push('","type":"b"});');
script.text = line.join('');
publ.$elements.shareLine.append(script);
};
But in line-button.js media_line_me.LineButton attaches a listener to the window.onload event, which by this time has already happened.
I really want to make sure that everything else is done before any social media widgets start to load. But right now I'm kind of out of ideas.
Is there another solution to this that's not loading the JS file as text, rewriting it in our script and then eval() it?
Here is your perfect answer
dynamic LINE share button
Github -- naver LINE dynamic share button
Dynamic share button adds:
css class for both <a> and <img>
All options are now meta properties
loads async, no interaction needed!!
Example usage
<!doctype html>
<html xmlns:og="http://ogp.me/ns#">
<head>
<meta name="naver-line-selector" content="" />
<meta property="og:locale" content="en_US" />
<meta property="og:url" content="http://www.google.com" />
<meta property="og:title" content="Dynamic Share button 0" />
<script src="jquery.js"></script>
<script>
jQuery(document).ready(function() {
$('meta[property="og\\:url"]').attr('content', 'https://www.google.com');
$('meta[property="og\\:title"]').attr('content', 'Lets exchange LINE');
$('meta[property="og\\:locale"]').attr('content', 'en_US');
$('meta[name="naver-line-selector"]').attr('content', '#after-this');
(function(d) {
var po = d.createElement('script');
po.type = 'text/javascript'; po.async = true;
po.src = 'naver-LINE-share-button.js?v=20140411';
var s = d.getElementsByTagName('script')[0];
s.parentNode.insertBefore(po, s);
})(document);
});
</script>
</head>
<body>
<div id="after-this"></div>
</body>
</html>
Please note, implementation is pure javascript. jQuery $.ready used only on html for convenience
Call to action
Could use the following
A glossy button with rounded corners
spreading this javascript library far and wide
The problem I am having is preventing a link from launching a new page and loading its href value in an iframe. The client site is using healcode a service that provides widgets to schedule fitness classes etc.Check this page When you click the signup button after the widget loads it opens a new page. What the client wants is to open that link on the same page or in a popup window. I have tried everything I can think of. I have used jQuery,fancybox, etc.
HERE IS THE CODE IM USING
screenshot.
What I think the problem is, is that my inline scripts loads before the widget scripts finish renders the the schedule html.
I know it is possible because this site uses the same widget and their signup opens the link in a overlay frame on the same page.
Please shed some light on this.
UPDATE:
<html>
<head>
<title>Test</title>
</head>
<body>
<iframe id="openViewHere" width="500px" height="500px" src="" ></iframe>
<script type="text/javascript">
healcode_widget_id = "7696499e81";
healcode_widget_name = "schedules";
healcode_widget_type = "mb";
document.write(unescape("%3Cscript src='https://www.healcode.com/javascripts/hc_widget.js' type='text/javascript'%3E%3C/script%3E"));
// Healcode Schedule Widget for Iyengar Yoga Association of Greater New York : Brooklyn Daily Class Schedule
</script>
<noscript>Please enable Javascript in order to get HealCode functionality</noscript>
<script src="https://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(".signup_now").click(function(){
var url = $(this).attr("href");
$("#openViewHere").attr("href",url);
return false;
});
</script>
</body>
</html>
Just use this code :
$( document ).ready(function(){
$("body").on('click',".signup_now",function(e){
$(this).attr("target","_self")
})
})
be aware that windows.ready() might fire , while the widget script is still adding elements dynamicaly that is why the listeners dont work , you have to access the element through already loaded parent ( e.g "body" ).
i have a html file with iframe and button in it, Is there a way to add a javascript function inside the body of iframe after I click the button?. Here is my code. Thanks
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script>
function callMe() {
var frame = $('iframe'),
contents = frame.contents(),
body = contents.find('body');
var script2 = document.createElement("script");
script2.type = "text/javascript";
script2.text = " function setEmployeeId(){var employeeId = 0;};"
$(body).append(script2);
};
</script>
<title>sample</title>
</head>
<body>
<iframe src="page2.html">
</iframe>
<button onClick="callMe();">click</button>
</body>
</html>
The result I want is to be like this.
<html>
<head>
<title>sample</title>
</head>
<body>
<iframe>
<html>
<head>
<title></title>
</head>
<body>
<script>
function setemployeeId() {
var employeeId = 0;
};
</script>
</body>
</html>
</iframe>
</body>
</html>
Hopefully you can clarify a few things for me as we go, but I think I have some bad news for you.
Any content that is loaded in an iframe cannot truly edited unless you own the page that is being loaded, in that case you can just bake in whatever you need into the loaded page.
HOWEVER!
You can still access elements in the iframe by using the contentWindow attribute. That is all laid out for you here: How to pick element inside iframe using document.getElementById
Once you've got the element you want to work with, you can create a function in the parent window and then add a call to the parent window's function using window.parent. That's outlined here: Calling a parent window function from an iframe
So if you wanted to make a button in an iframe alter the contents of the iframe you could use
var elem = document.getElementById('myframe1').contentWindow.document.getElementById('myButton')
and then create a function in your parent window
function changeIt(){
document.getElementById('thingToChangeInIframe_ItsIDintheIframe').property = 'value';
}
and append it to the button with the code
elem.setAttribute('onclick', 'changeIt();');
If you have any clarifications to what you need just comment and we'll work those out. I'm sorry this doesn't use much jQuery but that's not really my forte, but I think the pure javascript is relatively self explanatory.
EDIT: I should clarify that if the iframe is on another domain then your options are pretty much all eliminated. For security reasons you can't mess with the settings on other people's pages when you load them in an iframe.
I've built a webpage that is basically a main-page with a div that is filled with different pages by using AJAX. This basically works by loading pages into a div by using innerHTML. One problem I ran into was when a page with javascript is loaded into that div all of the other code runs fine; just the javascript doesnt work.
Main-page(index.php):
<html>
<head>
<script type="text/java">
////bunch of functions////
////Ends up that page_request on this instance is 'graph.php'////
document.getElementById('mydiv').innerHTML=page_request.responseText
</script>
</head>
<body>
<div id="mydiv"><div>
</body>
</html>
Child-page(loaded in div(graph.php)):
<html>
<head>
<link rel="stylesheet" href="mystyle.css" type="text/css" media="screen" />
<script src="other_stuff.js"></script>
</head>
<body>
<script>
///bunch of script////
</script>
</body>
</html>
Now when loading the page itself (opening graph.php) I notice that everything works fine; it is just when I import graph.php to index.php through innerHTML into my div it does not work (no errors just nothing is shown). I have read through many other posts and guides and did not come up with any distictive solution; thinks I have seen were:
Put eval() around my code [I saw on a guide that this could lead
to malicious user attacks].
Create the scripts on the main page then just import the data using:
document.createElement() and .parentNode.insertBefore()
Create a listener and call the functions when I open graph.php
And this good example
Even though I am not 100% sure how this example could work because I have php populate information for the javascript to collect and then make my graph on graph.php; so if I put that function into index.php the php will already be loaded so I would have to refresh the page or call them to update information somehow. Just for some context I am ok at php but I am new and struggle with javascript so I do not know what solution would fit my situation or work the best. Any tips/examples would be greatly appreciated, thank you for your time.
From you code snippets it seems you're looking to embed complete pages within the main page. If that's the case, a more straightforward approach would be to use an iframe element instead.
For example:
...
<div id="main-page-container">
<iframe src="some-path/graph.php" scrolling="no" frameborder="no"></iframe>
</div>
...
See reference and usage example.
I would suggest using jQuery's .load() function for this.
Take a look here: jQuery API
Older browsers such as IE8 and below don't allow you insert a string that contains javascript and execute it, in any form.
Take for instance:
function addScriptText(js_code) {
var element = document.createElement("script");
element.innerHTML = js_code;
document.head.appendChild(element);
}
will not work in IE8 and below.
You must use eval to accomplish this:
function addScriptText(js_code) {
window.eval.call(window, js_code);
}
Otherwise you need to dynamically request an external js file such as:
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "externalScript.js";
document.getElementsByTagName("head")[0].appendChild(script);
Note: The page you are loading (page2.html in this example) must be on the same domain as the page that is loading it (page1.html in this example)
Working solution with jQuery:
Page 1:
<!DOCTYPE html>
<html>
<head>
<title>Page 1</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#page2").load("page2.html");
});
</script>
</head>
<body>
<h1>Page 1 Header</h1>
<div id="page2">
</div>
</body>
</html>
Page 2:
<h2>Page 2 Header</h2>
<script type="text/javascript">
alert("Page 2 loaded and javascript executed!");
</script>
I'm trying to optimize the page render and download and I'm stuck in this situation...
I'd like to load an advertise at the end of page load, I made a simple test PAGE
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
var AdBrite_Title_Color = '443E3E';
var AdBrite_Text_Color = '443E3E';
var AdBrite_Background_Color = 'D1CFCF';
var AdBrite_Border_Color = '443E3E';
var AdBrite_URL_Color = '443E3E';
try{var AdBrite_Iframe=window.top!=window.self?2:1;var AdBrite_Referrer=document.referrer==''?document.location:document.referrer;AdBrite_Referrer=encodeURIComponent(AdBrite_Referrer);}catch(e){var AdBrite_Iframe='';var AdBrite_Referrer='';}
$(document).ready(function(){
});
</script>
</head>
<body style="background: #90EE90;">
<div id="page" style="">
<div id="loginbox" style="position: fixed; top: 150px; left: 250px;">
<span style="white-space:nowrap;"> <!-- AD MUST BE HERE-->
<a target="_top"
href="http://www.adbrite.com/mb/commerce/purchase_form.php?opid=1866421&afsid=1">
<img src="http://files.adbrite.com/mb/images/adbrite-your-ad-here-banner-w.gif"
style="background-color:#443E3E;border:none;padding:0;margin:0;"
alt="Your Ad Here" width="11" height="60" border="0" />
</a></span>
</div>
</div>
</body>
</html>
Where there is the HTML Comment originally there were:
<script type="text/javascript">
document.write(String.fromCharCode(60,83,67,82,73,80,84));
document.write(' src="http://ads.adbrite.com/mb/text_group.php?sid=1866421&zs=3436385f3630&ifr='+AdBrite_Iframe+'&ref='+AdBrite_Referrer+'" type="text/javascript">');
document.write(String.fromCharCode(60,47,83,67,82,73,80,84,62));
</script>
I need to load this element after page using jquery, I tried many solution, binding document.write action, adding a <script></script> element but nothing works...
I really need an help ;)
At the end I solved my problem, loading an iframe after page load using jQuery...
Create a specific page for ADV, then loading the iframe
$(document).ready(function(){
$('#advtop').html('<iframe src="http://www.blablabla.ext/ad.php?pos=top"></iframe>');
});
The AD script won't slow the page load, and when the document is ready the iframe is placed in the correct position, without causing ADV replacing everything, also ADV get the correct URL referrer
Hope this could help someone...
I actually did this on the newsweek.com site using a script called writeCapture.js. It intercepts the document.write method and switches all the ad code to html injection (really fancy stuff!).
Anyway, to see a working example hit up (newsweek.com) thedailybeast.com and enter newsweek.ads.refresh() in the console. As for documentation, the writeCapture site will explain everything.
I do this on my site using the following code:
function adScript(){
var s1 = document.createElement('script');
s1.src='http://put_your_url_here';
s1.type = 'text/javascript';
s1.onload = function(){
// initialize the page to use the script
}
document.getElementsByTagName('head')[0].appendChild(s1);
}
adScript();
Essentially, the 'adScript' function is called in the '$(document).ready' jQuery event. This dynamically creates a script tag. The browser loads the remote script. Once loaded, your 'onload' function is executed.
Hope that helps.
Bob