I have included the Google+ share url in my website (not the +1). However, it shares the entire page and not the specific article I want to link to. My article title has an anchor id, but when I add it to the share's url, it doesn't pick it up...
The logo of the page and a short description shows up, but it won't link to the specific article.
Any help would be greatly appreciated!
As far as I know, Google+ share button currently works only for whole pages and not sections.
The only way, though not very user friendly, is to split each content into single pages.
Anyone - pls correct me if I am wrong.
You will need to use the share button rather than the share URL to be able to share links with anchors. Even URL encoding the anchors results in them being stripped from the URL that is shared with the link. When using the button, specify the href value to include the particular anchor that you need.
Another approach if share link is a requirement....
Would be to implement on your site an alternate method for the anchors. For example, use http://mysite.com/?article=myarticleid where myarticleid is the same as the anchor and check for that on page load (source).
function getParameter(param) {
var val = document.URL;
var url = val.substr(val.indexOf(param))
var n=url.replace(param+"=","");
alert(n);
}
var anchor;
function checkForAnchor(){
anchor = getParameter("myarticle");
// Do something with anchor, see code examples below.
}
window.onload = checkForAnchor;
Then add some JavaScript at the end of that checkForAnchor() function that will scroll the page on load to the correct location or just jump to the anchor.
In jQuery, you might animate that effect like (source):
jQuery('html,body').animate({scrollTop: jQuery('#' + anchor).offset()}, 1000);
In standard JavaScript, you could simply change the location and append the anchor:
window.location = '#'+id;
Related
I want to add a Youtube video to my site, which I can do via the "Content" field, and then pressing the "Insert Media" button, which is based on TinyMCE. It then asks me for the video URL, details & dimensions, etc.
However, I want to add some styling to that video; for example, the "Play" button make it a different color and add some JS when pressing that button.
Is there any advice you can give me to achieve this?
Thank you
I'll answer this question in 2 parts:
TinyMCE styles (probably not what you are looking for, but for the record)
You can add your own css file to tinyMCE that will be loaded inside the CMS. This is useful to make the Text in the CMS look similar to the frontend (eg Headline sizes).
The default file in 3.x is mysite/css/editor.css I think, but you can also overwrite that:
(Note that this editor.css file only applies inside the TinyMCE editor, not to your website, so it's only for the benefit of the content author/admin)
# mysite/_config/config.yml
LeftAndMain:
extensions:
- 'MyLeftAndMainExtension'
// mysite/code/MyLeftAndMainExtension.php
class MyLeftAndMainExtension extends \LeftAndMainExtension {
public function init() {
parent::init();
$editorCss = '/mysite/css/some-other-css-file.css';
// add a ?t=123456 timestamp to bust the cache
$editorCss .= '?t=' . filemtime(\Director::getAbsFile($editorCss));
\HtmlEditorConfig::get_active()->setOption('content_css', $editorCss);
}
}
Styling YouTube Videos
YouTube these days embeds as an <iframe>, which means you don't really get any option to apply CSS to it. So styling in the traditional way that you are looking for is not possible.
But there are 4 things you could do that give you a little bit of control:
A) There are some limited options provided by google that you can add to the src of the <iframe> to change the style of the video. Eg if you add autoplay=1 and controls=0: <iframe src="https://www.youtube.com/watch?v=dQw4w9WgXcQ?autoplay=1&controls=0">
All availalbe parameters are documented here: https://developers.google.com/youtube/player_parameters
B) You could kind of fake it by not actually embedding the video, but instead add a thumbnail image in the CMS and add a play button over it and make that image a link to the video. Then when the user clicks on it, open the real YouTube player in a popup or a new tab
C) You could use the YouTube JavaScript API https://developers.google.com/youtube/iframe_api_reference. It allows you to send commands to the player like "Play" / "Pause". This way you could embed the video with controls=0, but then create your own controls that send commands to the player. Your own controls can be styled what ever way you want. Though be warned, this is probably quiet a lot of work to implement.
D) Like C) but easier, you could look if there is an existing software that uses the YouTube API to do that for you. A quick search returned https://plyr.io/ for example. Maybe it's worth looking into
(Also note, B) and C) are not easily integrated into TinyMCE, I've done both approaches, but I added extra CMS Fields for the Video (TextField named VideoURL and ImageField named VideoPreviewImage) and than displayed that in the frontend next to the text.
Consider the following link:
https://bittrex.com/Market/Index?MarketName=USDT-neo
I need to somehow manipulate the URL so that when it's clicked, it jumps straight to the Order Book section of the webpage.
I've tried finding the ID for that part and appending it the URL, but that wouldn't work because the ID isn't generated when you enter the page through the normal URL.
Does anyone know how to manipulate the link so it jumps straight to the Order Book part?
To be clear, I do not want to use JS. I want to have a pure link which I can click and will take me to the Order Book.
I need this so I could take a screenshot of the chart using a node module called Pageres.
If anyone has any different idea as of how to download the chart of the Order Book to a png, it'd be awesome (Or even more generally, download a chart of any cryptocurrency's Order Book to a png, using any website).
Many thanks,
~Yuval
Using javascript, you can handle the click in the following way:
<a id="fake_link" class="fake-link" href="https://bittrex.com/Market/Index?MarketName=USDT-neo">Click me</a>
<script>
var elem = document.getElamentById("fake_link");
elem.addEventListener("click",function(e){
e.stopPropagation();
e.preventDefault();
window.location = "YOUR NEW LOCATION";
},false);
</script>
In the general case, all you need is add the id of the part of the page that you want to go to. The section you mention starts with:
<div class="row" id="rowTable">
So all you have to do is add #rowTable to your URL:
https://bittrex.com/Market/Index?MarketName=USDT-neo#rowTable
That will instruct the browser to look for the part of the page with the id that you mention.
But you are right, the id part of the page is generated after it is loaded. In your case it only works if you change the URL with the page already loaded.
You could try using an intermediate page with a bit of javascript that will first load the page, then jump to the part you want.
You can use the location.hash property in java script to grab the hash of the current page
use is like this:
var hash = window.location.hash;
if(hash == "#tabChartOrderBook"){
//function to switch tab
}
I wrote this for your url
https://bittrex.com/Market/Index?MarketName=USDT-neo#tabChartOrderBook
and as I said in code your url hash is #tabChartOrderBook
The best example I can find of this is the URL of a google images page with a particular image selected. The URL changes for each image that is selected.
In my case, I have a Notebooks model and a nested Notes model. In the view showing all the notes, when the user clicks a note he/she is taken to the Note show view. Instead, I would like them to be shown a partial within the same view, but I'd like the URL to change to include info about the selected note.
How do I do this?
You can do this with replaceState() in javascript. When the user clicks on the notes you open the note any way you want (using an ajax call for instance) and then when it's open you use a callback to fire the replaceState() function that will replace the current url with whatever you want (I suppose the url of your note).
Then, when a user comes into you site with this precise url you can parse the url to show the note he requested (or let Rails default behaviour redirect him to your note show view).
Here is the documentation for replaceState(): https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history#The_replaceState().C2.A0method
Here is a non working example of the corresponding view using jquery to illustrate my point:
= link_to "note", note_path(#note), id: 'x_link'
<script>
$('#x_link').click(function(e) {
e.preventDefault() //we do not let the event propagate
href = $(this).attr('href') //we get the href attribute of our note
$.get(href, function(data) {
show_partial(data) // write some method to display you partial
history.replaceState(null, null, href) //replace the current state
})
})
</script>
I am currently building a website containing lots of links to different sections of my website (also known as navigation). Lets call those links and their corresponding pages link1, page1 , link2, page2, link3, page3 etc.
The general code for them is this:
Link1
Link2
Link3
I want the user to click each link to move to the corresponding webpage and it works as supposed to. The problem is that I want the links to do nothing when the user is on the same page as the link they clicked (meaning it will only reload the page). Let me make this clear by using an example:
Current use: User is on page1. User clicks on link1. The browser will reload page1.
Desired use: User is on page1. User clicks on link1. The browser will do nothing.
TL;DR Essentially I am searching for an if clause. I have read there is no if clause in HTML simply because it's a markup language but what is another way to implement this? Thanks for your help.
The best answer I have found without the need to use JQuery or JS after munching through SO is this, answer made by Matt Crinklaw-Vogt:
Add a /#!. This will prevent the scrolling to the top and will also prevent the page reloading.
Code:
Link1
Original answer:
How do I stop a web page from scrolling to the top when a link is clicked that triggers JavaScript?
I'm not sure I like it, but you could use an empty bookmark to do this...
Link1
If you aren't on page1, it will load page1, but once you are there, it won't do anything.
Link2
OR
<a onclick = "return false;">Link2</a>
That cancels out the load.
On page load, you could use JS as follows
Link1
JS:
document.getElementById("link1").setAttribute("href", "#");
You can use javascript to compare the current url to the anchor link, and if they're the same, prevent the default click event. Using jquery:
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/\/$/,'') + "$");
$('a').each(function(){
if(urlRegExp.test(this.href.replace(/\/$/,''))) {
$(this).click(function(event) {
event.preventDefault();
});
}
});
I'm developing a web app. In it I have a section called categories that every time a user clicks one of the categories an update panel loads the appropriate content.
After the user clicked the category I want to change the browser's address bar url from
www.mysite.com/products
to something like
www.mysite.com/products/{selectedCat}
without refreshing the page.
Is there some kind of JavaScript API I can use to achieve this?
With HTML5 you can modify the url without reloading:
If you want to make a new post in the browser's history (i.e. back button will work)
window.history.pushState('Object', 'Title', '/new-url');
If you just want to change the url without being able to go back
window.history.replaceState('Object', 'Title', '/another-new-url');
The object can be used for ajax navigation:
window.history.pushState({ id: 35 }, 'Viewing item #35', '/item/35');
window.onpopstate = function (e) {
var id = e.state.id;
load_item(id);
};
Read more here: http://www.w3.org/TR/html5-author/history.html
A fallback sollution: https://github.com/browserstate/history.js
To add to what the guys have already said edit the window.location.hash property to match the URL you want in your onclick function.
window.location.hash = 'category-name'; // address bar would become http://example.com/#category-name
I believe directly manipulating the address bar to a completely different url without moving to that url isn't allowed for security reasons, if you are happy with it being
www.mysite.com/products/#{selectedCat}
i.e. an anchor style link within the same page then look into the various history/"back button" scripts that are now present in most javascript libraries.
The mention of update panel leads me to guess you are using asp.net, in that case the asp.net ajax history control is a good place to start
I don't think this is possible (at least changing to a totally different address), as it would be an unintuitive misuse of the address bar, and could promote phishing attacks.
This cannot be done the way you're saying it. The method suggested by somej.net is the closest you can get. It's actually very common practice in the AJAX age. Even Gmail uses this.
"window.location.hash"
as suggested by sanchothefat should be the one and only way of doing it. Because all the places that I have seen this feature, it's all the time after the # in URL.