简体   繁体   中英

Parameter 'href' should represent a valid URL in facebook

在此处输入图片描述Facebook document.getElementById("shareFB").setAttribute("href", " https://www.facebook.com/sharer/sharer.php?u= " + document.URL);

                        </li>

Are your using localHost. ? if you are using some plugins for share and you are in localHost then that's the issue. It will work fine when u deploy

In your url, you have ' https://www.facebook.com/sharer/sharer.php?u= ...' . You should try replacing 'u' with 'href' in this url.

Try This...

 function sethref() { document.getElementById("shareFB").setAttribute("href","https://www.facebook.com/sharer/sharer.php?u=" + encodeURIComponent(document.URL)); } window.onload = sethref;
 <!DOCTYPE html> <html> <title>Web Page Design</title> <body> <a id="shareFB">FB</a> </body> </html>

Are you doing this at your local development environment?

I'm getting this error on localhost but it works OK when a normal URL is supplied. Here's a dummy implementation. This works on my application during development:

const share = 'https://test-location.com';
const appId = 'your app id here';

<a
href=`https://www.facebook.com/sharer/sharer.php?app_id=${appId}&sdk=joey&u=${share}&display=popup&ref=plugin&src=share_button"
target="_blank"
>
Share
</a>

您应该对传递的 URL 进行转义或编码

document.getElementById("shareFB").setAttribute("href", "https://www.facebook.com/sharer/sharer.php?u=" + encodeURIComponent(document.URL));

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