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
Related
I am completely new to javascript and would like to show and refresh RSS feed in a web page. I managed to show it in the page but I have problem refreshing the feed.
I tried the suggestions from this forum using setinterval and settimeout together with head.appendChild but did not work. They do not refresh the page.
I also tried "location.reload" function, which refreshes the page but shows blank page after the refresh. The same with metadata refresh in HTML.
I will be really grateful if someone can suggest a solution. My latest code is below which shows the feed in the page but does not refresh.
Edit: I may also need to add that the page I am showing the results are inside a PowerBI dasboard (web content tile). It is similar to w3schools trial windows. I could not refresh the results in any of them.
Thanks,
<!doctype html>
<html>
<head>
</head>
<body>
<script type = "text/JavaScript">
function load_js()
{
var versionUpdate = (new Date()).getTime();
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type = "text/javascript";
script.src= '//rss.bloople.net/?url=http%3A%2F%2Frss.cnn.com%2Frss%2Fcnn_latest.rss&detail=-1&limit=10&showtitle=false&type=js'+
versionUpdate;
head.appendChild(script);
script.remove;
}
load_js();
setInterval(function(){
load_js();
}, 3000);
</script>
</body>
</html>
Since you're creating the <script> tag on every iteration, you won't be able to update te source.
Consider remembering the <script> to update the src;
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<script type = "text/JavaScript">
this.tag = document.createElement('script');
function load_js() {
console.log('load_js()');
var versionUpdate = (new Date()).getTime();
var head= document.getElementsByTagName('head')[0];
this.tag.type = "text/javascript";
this.tag.src= '//rss.bloople.net/?url=http%3A%2F%2Frss.cnn.com%2Frss%2Fcnn_latest.rss&detail=-1&limit=10&showtitle=false&type=js'+ versionUpdate;
head.appendChild(this.tag);
this.tag.remove;
}
load_js();
setInterval(function(){
load_js();
}, 3000);
</script>
</body>
</html>
I've tested this in a w3schools trial windows and the versionUpdate is updated on each iteration.
JsBIN Demo
I create a Badge on Google plus developers : https://developers.google.com/+/web/badge/
but when I copy and past the code into my HTML file the badge not showing. Here is my code :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Google + Badge</title>
</head>
<body>
<!-- Place this tag where you want the widget to render. -->
<div class="g-page" data-href="https://plus.google.com/109600806421917664383" data-rel="publisher"></div>
<!-- Place this tag after the last widget tag. -->
<script type="text/javascript">
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/platform.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
</body>
</html>
What I'm doing wrong !? Thanks
adeneo's JSFiddle wasn't working for me either (Chrome 35, OSX). First I had to whitelist it in the Disconnect extension. Then I was getting the console error
Uncaught ReferenceError iframes is not defined
which, according to http://www.jesse-smith.net/fixed-google-plus-1-button-working/ and Google PlusOne Button has errors on Chrome, is caused by having 3rd-party cookies blocked.
If you're using Chrome as well (and the problem is caused for you by blocking 3rd-party cookies as well), there should be a cookie icon on the right-hand side of your URL bar. Click it, click "Show cookies and other site data...", change to the "Blocked" tab and allow all the 3rd-party cookies from *.google.com domains.
You can also go Settings > Show advanced settings > Privacy, click the “Content Settings” button and enable 3rd-party cookies everywhere.
Unfortunately I wasn't able to find a way to get your G+ button to work in browsers with 3rd-party cookies disabled.
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.
How can I distinguish from dynamic loaded script and normal script included using src property? For example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script src="static.js" type="text/javascript"></script>
<script type="text/javascript">
document.getElementsByTagName('head')[0].appendChild(document.createElement('script').src = 'dynamic.js');
</script>
</head>
<body>
<script type="text/javascript">
// I want to distinguish dynamic.js and static.js script tags
</script>
</body>
</html>
Add a specific HTML5 data-* attribute to the dynamic scripts:
var script = document.createElement("script");
script.setAttribute("data-is-dynamic", "true");
script.id = "script1";
script.src = "dynamic.js";
document.getElementsByTagName('head')[0].appendChild(script);
An option for getting the <head> element is to use document.head, although it's not supported in older browsers. Here's the MDN docs - https://developer.mozilla.org/en-US/docs/DOM/document.head
Then to distinguish later, use:
var someScript = document.getElementById("script1"); // or any script tag you want to analyze
if (someScript.getAttribute("data-is-dynamic") === "true") {
// is dynamic
} else {
// is static
}
Of course, at the same time, you can set an attribute on static script tags, like data-is-static, and then check for that in the opposite manner.
This allows you to validly set a "flag" on a script tag that you can retrieve later.
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.