简体   繁体   中英

twitter share button with dynamic url through JS

I am getting a headache getting this to work. I managed to do it with fb, but twitter doesn't work.

I made a js function that takes the html path of a page with a few other parameters, and I just want to use that function instead of manually writing the url. Problem is, twitter stops at the text, and doesn't write the url given by "htmlpath".

{ window.open(" http://twitter.com/intent/tweet?text=An%20Awesome%20Link&url= " + htmlpath); }

If I would hazard a guess, it would be that you need to prepend the protocol (most likely http:// or https:// ). If the protocol is not present, Twitter will not add a link.

// maybe something like this?
window.open(
    "http://twitter.com/intent/tweet?text=An%20Awesome%20Link&url=" + 
    ((htmlpath.substr(0,4) == 'http')?htmlpath:'http://' + htmlpath)
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM