获取当前域名 点击打开url a标签

import { getCurrentDomain, openUrl, verification } from "@/utils/methods";

//点击下载
download(urlId) {
  let url = `${getCurrentDomain()}${this.dowmloadApi}${urlId}`
  openUrl(url)
},

js方法封装:

/utils/methods.js

/**
 * @description: 获取当前域名,如http://www.baidu.com
 * @param {*}
 * @return {*}
 */
export function getCurrentDomain() {
  return window.location.protocol + '//' + window.location.host
}
/**
 * @description: 打开url
 * @param {*} url
 * @return {*}
 */
export function openUrl(url) {
  let a = document.createElement('a')
  a.setAttribute('href', url)
  a.setAttribute('target', '_blank')
  a.setAttribute('id', 'd2admin-link-temp')
  document.body.appendChild(a)
  a.click()
  document.body.removeChild(document.getElementById('d2admin-link-temp'))
}
//js 生成a标签 post
function post(url, params) {
    var temp = document.createElement("form");
    temp.action = url;        
    temp.method = "post";        
    temp.style.display = "none";        
    for (var x in params) {        
        var opt = document.createElement("textarea");        
        opt.name = x;        
        opt.value = params[x];        
        temp.appendChild(opt);        
    }        
    document.body.appendChild(temp);        
    temp.submit();        
    return temp;
}
//范例:<a href="javascript:;" onclick="javascript:post('/dosomething', {id:1,name:'Jack'})">test</a>
//ajax原生绑定
HttpHelper.ajaxPost=function(url,data){
    var backData;
    $.ajax({
        beforeSend: function(request) {
            request.setRequestHeader("Authorization",localStorage.getItem("messageId"));
            request.setRequestHeader("test-user-id","e8458684-7572-4130-b63b-a5c538ba8898");
        },
        type: 'POST',  // 请求方式
        url: prefixUrl+url,
        dataType: "json",  // 服务器响应的数据类型
        'data':data,
        async: false,
        success: function (data) {
        }
    }).done(
        function(response) {
            backData = response;
        });
    return backData;;
}
posted @ 2021-12-01 15:05  yoona-lin  阅读(312)  评论(0编辑  收藏  举报