본문 바로가기
웹 개발

[Safari(ios/mac)] location.href 안될때( 다운로드 안될 때 )

by 배추잠자리 2022. 3. 14.
반응형

사파리에서 a href download가 안되는 경우

# a href download attribute doesn't working in latest safari

1. webview 다운로드 링크 안될 때 
2. pywebview 다운로드 링크 안될 때
3. ios에서 다운로드 링크 안될 때
4. mac에서 다운로드 링크 안될 때
5. 사파리(safari)에서 다운로드 링크 안될 때
 

■ 소스코드

let _a = document.createElement('a');
_a.target = '_blank';
_a.href = _url;
document.body.appendChild(_a);
_a.click();


webview 또는 mac, ios, pywebview 에서 다운로드나 링크이동이 안되는 경우가 있다.
safari 기반이라면 당연히 안될것이고~!
링크이동이나 다운로드를 해야된다면 저런식으로 추가해주면 된다.

 


# MAC에서만 처리를 하고 윈도우는 그대로 하고 싶을 때.
■ 소스코드

let _osCheck = navigator.userAgent;
if(_osCheck.includes('Mac')) {
    let _a = document.createElement('a');
    _a.target = '_blank';
    _a.href = _url;
    document.body.appendChild(_a);
    _a.click();
}else{
    location.href = _url;
}

navigator.userAgent 해당 운영체제를 확인할 수 있다.
따라서 Mac 일때만 href 다운로드 처리를 다른 방식으로 해주고, 윈도우에서는 location.href 를 사용하도록 분기할 수 있다.

[keyword]
Safari location.href, 사파리 location.href, 자바스크립트 OS 체크, 자바스크립트 OS 확인, safari href, 사파리 href, ios href, javascript os 확인, 사파리 href 다운로드, safari download, safari href download, 사파리 href download, 모바일 safari href

반응형

댓글